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