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