-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathEach.php
222 lines (207 loc) · 7.26 KB
/
Each.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule;
use Attribute;
use Closure;
use JetBrains\PhpStorm\ArrayShape;
use Yiisoft\Validator\AfterInitAttributeEventInterface;
use Yiisoft\Validator\DataSet\ObjectDataSet;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\Helper\PropagateOptionsHelper;
use Yiisoft\Validator\Helper\RulesDumper;
use Yiisoft\Validator\Helper\RulesNormalizer;
use Yiisoft\Validator\PropagateOptionsInterface;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\ValidatorInterface;
use Yiisoft\Validator\WhenInterface;
/**
* Allows to define a set of rules for validating each element of an iterable.
*
* An example for simple iterable that can be used to validate RGB color:
*
* ```php
* $rules = [
* new Count(3), // Not required for using with `Each`.
* new Each([
* new Integer(min: 0, max: 255),
* // More rules can be added here.
* ]),
* ];
* ```
*
* When paired with {@see Nested} rule, it allows validation of related data:
*
* ```php
* $coordinateRules = [new Number(min: -10, max: 10)];
* $rule = new Each([
* new Nested([
* 'coordinates.x' => $coordinateRules,
* 'coordinates.y' => $coordinateRules,
* ]),
* ]);
* ```
*
* It's also possible to use DTO objects with PHP attributes, see {@see ObjectDataSet} documentation and guide for
* details.
*
* Supports propagation of options (see {@see PropagateOptionsHelper::propagate()} for available options and
* requirements).
*
* @see EachHandler Corresponding handler performing the actual validation.
*
* @psalm-import-type SkipOnEmptyValue from SkipOnEmptyInterface
* @psalm-import-type RawRules from ValidatorInterface
* @psalm-import-type NormalizedRulesMap from RulesNormalizer
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Each implements
DumpedRuleInterface,
SkipOnEmptyInterface,
SkipOnErrorInterface,
WhenInterface,
PropagateOptionsInterface,
AfterInitAttributeEventInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
use WhenTrait;
/**
* A name of parameter string current key in the {@see Each} rule
*/
public const PARAMETER_EACH_KEY = 'yii-validator-current-each-key';
/**
* @var array Normalized rules to apply for each element of the validated iterable.
*
* @psalm-var NormalizedRulesMap
*/
private array $rules;
/**
* @param callable|iterable|object|string $rules Rules to apply for each element of the validated iterable.
* They will be normalized using {@see RulesNormalizer}.
*
* @psalm-param RawRules $rules
*
* @param string $incorrectInputMessage Error message used when validation fails because the validated value is not
* an iterable.
*
* You may use the following placeholders in the message:
*
* - `{property}`: the translated label of the property being validated.
* - `{type}`: the type of the value being validated.
* @param string $incorrectInputKeyMessage Error message used when validation fails because the validated iterable
* contains invalid keys. Only integer and string keys are allowed.
*
* You may use the following placeholders in the message:
*
* - `{property}`: the translated label of the property being validated.
* - `{type}`: the type of the iterable key being validated.
* @param bool|callable|null $skipOnEmpty Whether to skip this `Each` rule with all defined {@see $rules} if the
* validated value is empty / not passed. See {@see SkipOnEmptyInterface}.
* @param bool $skipOnError Whether to skip this `Each` rule with all defined {@see $rules} if any of the previous
* rules gave an error. See {@see SkipOnErrorInterface}.
* @param Closure|null $when A callable to define a condition for applying this `Each` rule with all defined
* {@see $rules}. See {@see WhenInterface}.
*
* @psalm-param SkipOnEmptyValue $skipOnEmpty
* @psalm-param WhenType $when
*/
public function __construct(
callable|iterable|object|string $rules = [],
private string $incorrectInputMessage = '{Property} must be array or iterable. {type} given.',
private string $incorrectInputKeyMessage = 'Every iterable key of {property} must have an integer or a ' .
'string type. {type} given.',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->rules = RulesNormalizer::normalize($rules);
$this->skipOnEmpty = $skipOnEmpty;
}
public function getName(): string
{
return self::class;
}
public function propagateOptions(): void
{
foreach ($this->rules as $key => $propertyRules) {
$this->rules[$key] = PropagateOptionsHelper::propagate($this, $propertyRules);
}
}
/**
* Gets a set of rules that needs to be applied to each element of the validated iterable.
*
* @return array A set of rules.
*
* @psalm-return NormalizedRulesMap
*
* @see $rules
*/
public function getRules(): array
{
return $this->rules;
}
/**
* Gets error message used when validation fails because the validated value is not an iterable.
*
* @return string Error message / template.
*
* @see $incorrectInputMessage
*/
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
/**
* Error message used when validation fails because the validated iterable contains invalid keys.
*
* @return string Error message / template.
*
* @see $incorrectInputKeyMessage
*/
public function getIncorrectInputKeyMessage(): string
{
return $this->incorrectInputKeyMessage;
}
#[ArrayShape([
'incorrectInputMessage' => 'array',
'incorrectInputKeyMessage' => 'array',
'skipOnEmpty' => 'bool',
'skipOnError' => 'bool',
'rules' => 'array',
])]
public function getOptions(): array
{
return [
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => [],
],
'incorrectInputKeyMessage' => [
'template' => $this->incorrectInputKeyMessage,
'parameters' => [],
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
'rules' => RulesDumper::asArray($this->rules),
];
}
public function getHandler(): string
{
return EachHandler::class;
}
public function afterInitAttribute(object $object): void
{
foreach ($this->rules as $propertyRules) {
foreach ($propertyRules as $rule) {
if ($rule instanceof AfterInitAttributeEventInterface) {
$rule->afterInitAttribute($object);
}
}
}
}
}