|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use JetBrains\PhpStorm\Pure; |
|
10
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
11
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
12
|
|
|
use ReflectionProperty; |
|
13
|
|
|
use Traversable; |
|
14
|
|
|
use Yiisoft\Translator\TranslatorInterface; |
|
15
|
|
|
use Yiisoft\Validator\DataSet\ArrayDataSet; |
|
16
|
|
|
use Yiisoft\Validator\DataSet\ObjectDataSet; |
|
17
|
|
|
use Yiisoft\Validator\DataSet\SingleValueDataSet; |
|
18
|
|
|
use Yiisoft\Validator\Rule\Callback; |
|
19
|
|
|
use Yiisoft\Validator\Rule\Trait\PreValidateTrait; |
|
20
|
|
|
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider; |
|
21
|
|
|
|
|
22
|
|
|
use function is_array; |
|
23
|
|
|
use function is_callable; |
|
24
|
|
|
use function is_int; |
|
25
|
|
|
use function is_object; |
|
26
|
|
|
use function is_string; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Validator validates {@link LimitInterface} against rules set for data set attributes. |
|
30
|
|
|
*/ |
|
31
|
|
|
final class Validator implements ValidatorInterface |
|
32
|
|
|
{ |
|
33
|
|
|
use PreValidateTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var callable |
|
37
|
|
|
*/ |
|
38
|
|
|
private $defaultSkipOnEmptyCallback; |
|
39
|
|
|
|
|
40
|
685 |
|
public function __construct( |
|
41
|
|
|
private RuleHandlerResolverInterface $ruleHandlerResolver, |
|
42
|
|
|
private TranslatorInterface $translator, |
|
43
|
|
|
/** |
|
44
|
|
|
* @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in |
|
45
|
|
|
* {@see validate()} method. |
|
46
|
|
|
*/ |
|
47
|
|
|
private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE |
|
48
|
|
|
| ReflectionProperty::IS_PROTECTED |
|
49
|
|
|
| ReflectionProperty::IS_PUBLIC, |
|
50
|
|
|
bool|callable|null $defaultSkipOnEmpty = null, |
|
51
|
|
|
) { |
|
52
|
685 |
|
$this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param DataSetInterface|mixed|RulesProviderInterface $data |
|
57
|
|
|
* @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RuleInterface|RulesProviderInterface|null $rules |
|
|
|
|
|
|
58
|
|
|
* |
|
59
|
|
|
* @throws ContainerExceptionInterface |
|
60
|
|
|
* @throws NotFoundExceptionInterface |
|
61
|
|
|
*/ |
|
62
|
705 |
|
public function validate(mixed $data, iterable|object|string|null $rules = null): Result |
|
63
|
|
|
{ |
|
64
|
705 |
|
$data = $this->normalizeDataSet($data); |
|
65
|
705 |
|
if ($rules === null && $data instanceof RulesProviderInterface) { |
|
66
|
27 |
|
$rules = $data->getRules(); |
|
67
|
682 |
|
} elseif ($rules instanceof RulesProviderInterface) { |
|
68
|
2 |
|
$rules = $rules->getRules(); |
|
69
|
680 |
|
} elseif ($rules instanceof RuleInterface) { |
|
70
|
1 |
|
$rules = [$rules]; |
|
71
|
679 |
|
} elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) { |
|
72
|
|
|
$rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules(); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
705 |
|
$compoundResult = new Result(); |
|
76
|
705 |
|
$context = new ValidationContext($this, $data); |
|
77
|
705 |
|
$results = []; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var mixed $attribute |
|
81
|
|
|
* @var iterable<Closure|RuleInterface>|RuleInterface $attributeRules |
|
82
|
|
|
*/ |
|
83
|
705 |
|
foreach ($rules ?? [] as $attribute => $attributeRules) { |
|
84
|
694 |
|
$result = new Result(); |
|
85
|
|
|
|
|
86
|
694 |
|
if (!is_iterable($attributeRules)) { |
|
87
|
619 |
|
$attributeRules = [$attributeRules]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
694 |
|
$attributeRules = $this->normalizeRules($attributeRules); |
|
91
|
|
|
|
|
92
|
694 |
|
if (is_int($attribute)) { |
|
93
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
94
|
604 |
|
$validatedData = $data->getData(); |
|
95
|
94 |
|
} elseif (is_string($attribute)) { |
|
96
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
97
|
94 |
|
$validatedData = $data->getAttributeValue($attribute); |
|
98
|
94 |
|
$context = $context->withAttribute($attribute); |
|
99
|
|
|
} else { |
|
100
|
|
|
$message = sprintf( |
|
101
|
|
|
'An attribute can only have an integer or a string type. %s given.', |
|
102
|
|
|
get_debug_type($attribute), |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
throw new InvalidArgumentException($message); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
694 |
|
$tempResult = $this->validateInternal($validatedData, $attributeRules, $context); |
|
109
|
|
|
|
|
110
|
666 |
|
foreach ($tempResult->getErrors() as $error) { |
|
111
|
386 |
|
$result->addError($error->getMessage(), $error->getParameters(), $error->getValuePath()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
666 |
|
$results[] = $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
677 |
|
foreach ($results as $result) { |
|
118
|
666 |
|
foreach ($result->getErrors() as $error) { |
|
119
|
386 |
|
$compoundResult->addError( |
|
120
|
386 |
|
$this->translator->translate($error->getMessage(), $error->getParameters()), |
|
121
|
386 |
|
$error->getParameters(), |
|
122
|
386 |
|
$error->getValuePath() |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
677 |
|
if ($data instanceof PostValidationHookInterface) { |
|
128
|
|
|
$data->processValidationResult($compoundResult); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
677 |
|
return $compoundResult; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
705 |
|
#[Pure] |
|
135
|
|
|
private function normalizeDataSet(mixed $data): DataSetInterface |
|
136
|
|
|
{ |
|
137
|
705 |
|
if ($data instanceof DataSetInterface) { |
|
138
|
69 |
|
return $data; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
641 |
|
if (is_object($data)) { |
|
142
|
38 |
|
return new ObjectDataSet($data); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
607 |
|
if (is_array($data)) { |
|
146
|
109 |
|
return new ArrayDataSet($data); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
524 |
|
return new SingleValueDataSet($data); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param iterable<RuleInterface> $rules |
|
154
|
|
|
*/ |
|
155
|
694 |
|
private function validateInternal(mixed $value, iterable $rules, ValidationContext $context): Result |
|
156
|
|
|
{ |
|
157
|
694 |
|
$compoundResult = new Result(); |
|
158
|
694 |
|
foreach ($rules as $rule) { |
|
159
|
694 |
|
if ($this->preValidate($value, $context, $rule)) { |
|
160
|
27 |
|
continue; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
688 |
|
$ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName()); |
|
164
|
686 |
|
$ruleResult = $ruleHandler->validate($value, $rule, $context); |
|
165
|
660 |
|
if ($ruleResult->isValid()) { |
|
166
|
297 |
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
386 |
|
$context->setParameter($this->parameterPreviousRulesErrored, true); |
|
170
|
|
|
|
|
171
|
386 |
|
foreach ($ruleResult->getErrors() as $error) { |
|
172
|
386 |
|
$valuePath = $error->getValuePath(); |
|
173
|
386 |
|
if ($context->getAttribute() !== null) { |
|
174
|
67 |
|
$valuePath = [$context->getAttribute(), ...$valuePath]; |
|
175
|
|
|
} |
|
176
|
386 |
|
$compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePath); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
666 |
|
return $compoundResult; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param iterable<Closure|RuleInterface> $rules |
|
184
|
|
|
* |
|
185
|
|
|
* @return iterable<RuleInterface> |
|
186
|
|
|
*/ |
|
187
|
694 |
|
private function normalizeRules(iterable $rules): iterable |
|
188
|
|
|
{ |
|
189
|
694 |
|
foreach ($rules as $rule) { |
|
190
|
694 |
|
yield $this->normalizeRule($rule); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
694 |
|
private function normalizeRule(RuleInterface|Closure $rule): RuleInterface |
|
195
|
|
|
{ |
|
196
|
694 |
|
if (is_callable($rule)) { |
|
197
|
3 |
|
return new Callback($rule); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
694 |
|
if (!$rule instanceof RuleInterface) { |
|
201
|
|
|
throw new InvalidArgumentException( |
|
202
|
|
|
sprintf( |
|
203
|
|
|
'Rule should be either an instance of %s or a callable, %s given.', |
|
204
|
|
|
RuleInterface::class, |
|
205
|
|
|
get_debug_type($rule) |
|
206
|
|
|
) |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
694 |
|
if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) { |
|
211
|
607 |
|
$rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
694 |
|
return $rule; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|