Passed
Pull Request — master (#517)
by
unknown
02:32
created

Required   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 110
ccs 16
cts 16
cp 1
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandler() 0 3 1
A getName() 0 3 1
A getOptions() 0 12 1
A getNotPassedMessage() 0 3 1
A getMessage() 0 3 1
A __construct() 0 8 1
A getEmptyCondition() 0 3 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\SkipOnErrorTrait;
10
use Yiisoft\Validator\Rule\Trait\WhenTrait;
11
use Yiisoft\Validator\RuleWithOptionsInterface;
12
use Yiisoft\Validator\SkipOnErrorInterface;
13
use Yiisoft\Validator\WhenInterface;
14
15
/**
16
 * Contains a set of options to determine if the value is not empty according to {@see $emptyCriteria}. When rule-level
17
 * criteria is not set, a handler-level criteria ({@see RequiredHandler::$defaultEmptyCriteria}) is applied (which is
18
 * also customizable). In case of using attributes, the attribute must be present with passed non-empty value.
19
 *
20
 * With default settings in order for value to pass the validation it must satisfy all the following conditions:
21
 *
22
 * - Passed.
23
 * - Not `null`.
24
 * - Not an empty string (after trimming).
25
 * - Not an empty array.
26
 *
27
 * When using with other rules, it must come first.
28
 *
29
 * @see RequiredHandler Corresponding handler performing the actual validation.
30
 *
31
 * @psalm-type EmptyCriteriaType = callable(mixed,bool):bool
32
 * @psalm-import-type WhenType from WhenInterface
33
 */
34
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
35
final class Required implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface
36
{
37 37
    use SkipOnErrorTrait;
38
    use WhenTrait;
39
40
    /**
41
     * @var callable|null An empty criteria (either a callable or class implementing `__invoke()` method) used to
42
     * determine emptiness of the value. The signature must be like the following:
43
     *
44
     * ```php
45
     * function (mixed $value, bool $isAttributeMissing): bool
46
     * ```
47 37
     *
48
     * `$isAttributeMissing` is a flag defining whether the attribute is missing (not used / not passed at all).
49
     *
50 1
     * @psalm-var EmptyCriteriaType|null
51
     */
52 1
    private $emptyCondition;
53
54
    /**
55 16
     * @param string $message Error message used when validation fails because the validated value is empty.
56
     *
57 16
     * You may use the following placeholders in the message:
58
     *
59
     * - `{attribute}`: the translated label of the attribute being validated.
60 6
     * @param string $notPassedMessage Error message used when validation fails because the validated value is not
61
     * passed.
62 6
     *
63
     * You may use the following placeholders in the message:
64
     *
65
     * - `{attribute}`: the translated label of the attribute being validated.
66
     * @param callable|null $emptyCondition An empty criteria used to determine emptiness of the value.
67
     * @psalm-param EmptyCriteriaType|null $emptyCondition
68 45
     *
69
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
70 45
     * {@see SkipOnErrorInterface}.
71
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
72
     * @psalm-param WhenType $when
73 1
     */
74
    public function __construct(
75
        private string $message = 'Value cannot be blank.',
76
        private string $notPassedMessage = 'Value not passed.',
77 1
        callable|null $emptyCondition = null,
78
        private bool $skipOnError = false,
79
        private Closure|null $when = null,
80
    ) {
81 1
        $this->emptyCondition = $emptyCondition;
82
    }
83
84 1
    public function getName(): string
85
    {
86
        return 'required';
87
    }
88 46
89
    /**
90 46
     * Gets error message used when validation fails because the validated value is empty.
91
     *
92
     * @return string Error message / template.
93
     *
94
     * @see $message
95
     */
96
    public function getMessage(): string
97
    {
98
        return $this->message;
99
    }
100
101
    /**
102
     * Gets error message used when validation fails because the validated value is not passed.
103
     *
104
     * @return string Error message / template.
105
     *
106
     * @see $message
107
     */
108
    public function getNotPassedMessage(): string
109
    {
110
        return $this->notPassedMessage;
111
    }
112
113
    /**
114
     * Gets empty criteria used to determine emptiness of the value.
115
     *
116
     * @return callable|null Empty criteria.
117
     * @psalm-return EmptyCriteriaType|null
118
     *
119
     * @see $emptyCondition
120
     */
121
    public function getEmptyCondition(): ?callable
122
    {
123
        return $this->emptyCondition;
124
    }
125
126
    public function getOptions(): array
127
    {
128
        return [
129
            'message' => [
130
                'template' => $this->message,
131
                'parameters' => [],
132
            ],
133
            'notPassedMessage' => [
134
                'template' => $this->notPassedMessage,
135
                'parameters' => [],
136
            ],
137
            'skipOnError' => $this->skipOnError,
138
        ];
139
    }
140
141
    public function getHandler(): string
142
    {
143
        return RequiredHandler::class;
144
    }
145
}
146