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