Passed
Pull Request — master (#333)
by Sergei
02:38
created

Required   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 14
dl 0
loc 51
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerClassName() 0 3 1
A getName() 0 3 1
A getOptions() 0 7 1
A getNotPassedMessage() 0 3 1
A getMessage() 0 3 1
A __construct() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
/**
19
 * Validates that the specified value is neither null nor empty.
20
 */
21
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
22
final class Required implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
23
{
24
    use SkipOnEmptyTrait;
25
    use SkipOnErrorTrait;
26
    use WhenTrait;
27
28 38
    public function __construct(
29
        private string $message = 'Value cannot be blank.',
30
        private string $notPassedMessage = 'Value not passed.',
31
32
        /**
33
         * @var bool|callable|null
34
         */
35
        private $skipOnEmpty = null,
36
        private bool $skipOnError = false,
37
        /**
38
         * @var Closure(mixed, ValidationContext):bool|null
39
         */
40
        private ?Closure $when = null,
41
    ) {
42
    }
43
44 1
    public function getName(): string
45
    {
46 1
        return 'required';
47
    }
48
49 15
    public function getMessage(): string
50
    {
51 15
        return $this->message;
52
    }
53
54 5
    public function getNotPassedMessage(): string
55
    {
56 5
        return $this->notPassedMessage;
57
    }
58
59 1
    public function getOptions(): array
60
    {
61
        return [
62 1
            'message' => $this->message,
63 1
            'notPassedMessage' => $this->notPassedMessage,
64 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
65 1
            'skipOnError' => $this->skipOnError,
66
        ];
67
    }
68
69 40
    public function getHandlerClassName(): string
70
    {
71 40
        return RequiredHandler::class;
72
    }
73
}
74