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