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 Yiisoft\Validator\DataSet\ArrayDataSet; |
13
|
|
|
use Yiisoft\Validator\DataSet\ObjectDataSet; |
14
|
|
|
use Yiisoft\Validator\DataSet\MixedDataSet; |
15
|
|
|
use Yiisoft\Validator\Rule\Callback; |
16
|
|
|
use Yiisoft\Validator\Rule\Trait\PreValidateTrait; |
17
|
|
|
|
18
|
|
|
use function gettype; |
19
|
|
|
use function is_array; |
20
|
|
|
use function is_callable; |
21
|
|
|
use function is_int; |
22
|
|
|
use function is_object; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validator validates {@link DataSetInterface} against rules set for data set attributes. |
26
|
|
|
*/ |
27
|
|
|
final class Validator implements ValidatorInterface |
28
|
|
|
{ |
29
|
|
|
use PreValidateTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var callable |
33
|
|
|
*/ |
34
|
|
|
private $defaultSkipOnEmptyCallback; |
35
|
|
|
|
36
|
634 |
|
public function __construct( |
37
|
|
|
private RuleHandlerResolverInterface $ruleHandlerResolver, |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool|callable|null |
41
|
|
|
*/ |
42
|
|
|
$defaultSkipOnEmpty = null, |
43
|
|
|
) { |
44
|
634 |
|
$this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param DataSetInterface|mixed|RulesProviderInterface $data |
49
|
|
|
* @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null $rules |
50
|
|
|
*/ |
51
|
107 |
|
public function validate(mixed $data, ?iterable $rules = null): Result |
52
|
|
|
{ |
53
|
107 |
|
$data = $this->normalizeDataSet($data); |
54
|
107 |
|
if ($rules === null && $data instanceof RulesProviderInterface) { |
55
|
25 |
|
$rules = $data->getRules(); |
56
|
|
|
} |
57
|
|
|
|
58
|
107 |
|
$compoundResult = new Result(); |
59
|
107 |
|
$context = new ValidationContext($this, $data); |
60
|
107 |
|
$results = []; |
61
|
|
|
|
62
|
107 |
|
foreach ($rules ?? [] as $attribute => $attributeRules) { |
63
|
97 |
|
$result = new Result(); |
64
|
|
|
|
65
|
97 |
|
$tempRule = is_array($attributeRules) ? $attributeRules : [$attributeRules]; |
66
|
97 |
|
$attributeRules = $this->normalizeRules($tempRule); |
67
|
|
|
|
68
|
97 |
|
if (is_int($attribute)) { |
69
|
37 |
|
$validatedData = $data->getData(); |
70
|
37 |
|
$validatedContext = $context; |
71
|
|
|
} else { |
72
|
64 |
|
$validatedData = $data->getAttributeValue($attribute); |
73
|
64 |
|
$validatedContext = $context->withAttribute($attribute); |
74
|
|
|
} |
75
|
|
|
|
76
|
97 |
|
$tempResult = $this->validateInternal( |
77
|
|
|
$validatedData, |
78
|
|
|
$attributeRules, |
79
|
|
|
$validatedContext |
80
|
|
|
); |
81
|
|
|
|
82
|
92 |
|
$result = $this->addErrors($result, $tempResult->getErrors()); |
83
|
92 |
|
$results[] = $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
102 |
|
foreach ($results as $result) { |
87
|
92 |
|
$compoundResult = $this->addErrors($compoundResult, $result->getErrors()); |
88
|
|
|
} |
89
|
|
|
|
90
|
102 |
|
if ($data instanceof PostValidationHookInterface) { |
91
|
|
|
$data->processValidationResult($compoundResult); |
92
|
|
|
} |
93
|
|
|
|
94
|
102 |
|
return $compoundResult; |
95
|
|
|
} |
96
|
|
|
|
97
|
107 |
|
#[Pure] |
98
|
|
|
private function normalizeDataSet($data): DataSetInterface |
99
|
|
|
{ |
100
|
107 |
|
if ($data instanceof DataSetInterface) { |
101
|
47 |
|
return $data; |
102
|
|
|
} |
103
|
|
|
|
104
|
65 |
|
if (is_object($data)) { |
105
|
27 |
|
return new ObjectDataSet($data); |
106
|
|
|
} |
107
|
|
|
|
108
|
42 |
|
if (is_array($data)) { |
109
|
13 |
|
return new ArrayDataSet($data); |
110
|
|
|
} |
111
|
|
|
|
112
|
39 |
|
return new MixedDataSet($data); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $value |
117
|
|
|
* @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules |
118
|
|
|
* @param ValidationContext $context |
119
|
|
|
* |
120
|
|
|
* @throws ContainerExceptionInterface |
121
|
|
|
* @throws NotFoundExceptionInterface |
122
|
|
|
* |
123
|
|
|
* @return Result |
124
|
|
|
*/ |
125
|
97 |
|
private function validateInternal($value, iterable $rules, ValidationContext $context): Result |
126
|
|
|
{ |
127
|
97 |
|
$compoundResult = new Result(); |
128
|
97 |
|
foreach ($rules as $rule) { |
129
|
97 |
|
if ($rule instanceof BeforeValidationInterface) { |
130
|
94 |
|
$preValidateResult = $this->preValidate($value, $context, $rule); |
131
|
94 |
|
if ($preValidateResult) { |
132
|
15 |
|
continue; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
92 |
|
$ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName()); |
137
|
90 |
|
$ruleResult = $ruleHandler->validate($value, $rule, $context); |
138
|
87 |
|
if ($ruleResult->isValid()) { |
139
|
36 |
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
71 |
|
$context->setParameter($this->parameterPreviousRulesErrored, true); |
143
|
|
|
|
144
|
71 |
|
foreach ($ruleResult->getErrors() as $error) { |
145
|
71 |
|
$valuePath = $error->getValuePath(); |
146
|
71 |
|
if ($context->getAttribute() !== null) { |
147
|
50 |
|
$valuePath = [$context->getAttribute(), ...$valuePath]; |
148
|
|
|
} |
149
|
71 |
|
$compoundResult->addError($error->getMessage(), $valuePath); |
150
|
|
|
} |
151
|
|
|
} |
152
|
92 |
|
return $compoundResult; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $rules |
157
|
|
|
* |
158
|
|
|
* @return iterable<RuleInterface> |
159
|
|
|
*/ |
160
|
97 |
|
private function normalizeRules(iterable $rules): iterable |
161
|
|
|
{ |
162
|
97 |
|
foreach ($rules as $rule) { |
163
|
97 |
|
yield $this->normalizeRule($rule); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
97 |
|
private function normalizeRule($rule): RuleInterface |
168
|
|
|
{ |
169
|
97 |
|
if (is_callable($rule)) { |
170
|
3 |
|
return new Callback($rule); |
171
|
|
|
} |
172
|
|
|
|
173
|
97 |
|
if (!$rule instanceof RuleInterface) { |
174
|
|
|
throw new InvalidArgumentException( |
175
|
|
|
sprintf( |
176
|
|
|
'Rule should be either an instance of %s or a callable, %s given.', |
177
|
|
|
RuleInterface::class, |
178
|
|
|
gettype($rule) |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
97 |
|
if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) { |
184
|
92 |
|
$rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback); |
185
|
|
|
} |
186
|
|
|
|
187
|
97 |
|
return $rule; |
188
|
|
|
} |
189
|
|
|
|
190
|
92 |
|
private function addErrors(Result $result, array $errors): Result |
191
|
|
|
{ |
192
|
92 |
|
foreach ($errors as $error) { |
193
|
71 |
|
$result->addError($error->getMessage(), $error->getValuePath()); |
194
|
|
|
} |
195
|
92 |
|
return $result; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|