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