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