1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\DataSet\ArrayDataSet; |
8
|
|
|
use Yiisoft\Validator\DataSet\ScalarDataSet; |
9
|
|
|
use Yiisoft\Validator\Rule\Nested; |
10
|
|
|
|
11
|
|
|
use function is_array; |
12
|
|
|
use function is_object; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validator validates {@link DataSetInterface} against rules set for data set attributes. |
16
|
|
|
*/ |
17
|
|
|
final class Validator implements ValidatorInterface |
18
|
|
|
{ |
19
|
|
|
private ?FormatterInterface $formatter; |
20
|
|
|
|
21
|
12 |
|
public function __construct(?FormatterInterface $formatter = null) |
22
|
|
|
{ |
23
|
12 |
|
$this->formatter = $formatter; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param DataSetInterface|mixed|RulesProviderInterface $data |
28
|
|
|
* @param Rule[][] $rules |
29
|
|
|
* @psalm-param iterable<string, Rule[]> $rules |
30
|
|
|
*/ |
31
|
12 |
|
public function validate($data, iterable $rules = []): Result |
32
|
|
|
{ |
33
|
12 |
|
$data = $this->normalizeDataSet($data); |
34
|
|
|
|
35
|
12 |
|
if ($data instanceof RulesProviderInterface) { |
36
|
2 |
|
$rules = $data->getRules(); |
37
|
|
|
} |
38
|
|
|
|
39
|
12 |
|
$context = new ValidationContext($data); |
40
|
12 |
|
$result = new Result(); |
41
|
|
|
|
42
|
|
|
/** @psalm-var array<string, array<array-key, Rule>> $rules */ |
43
|
12 |
|
foreach ($rules as $attribute => $attributeRules) { |
44
|
12 |
|
$ruleSet = new RuleSet($attributeRules); |
45
|
|
|
|
46
|
12 |
|
if ($this->formatter !== null) { |
47
|
|
|
$ruleSet = $ruleSet->withFormatter($this->formatter); |
48
|
|
|
} |
49
|
|
|
|
50
|
12 |
|
if ($attributeRules[0] instanceof Nested) { |
51
|
|
|
$nesteds = $attributeRules[0]->getOptions(); |
52
|
|
|
|
53
|
|
|
foreach ($nesteds as $nested => $options) { |
54
|
|
|
$values[$nested] = $data->getAttributeValue($attribute . '.' . $nested); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$tempResult = $ruleSet->validate( |
58
|
|
|
$values, |
|
|
|
|
59
|
|
|
$context->withAttribute($attribute . '.' . $nested) |
|
|
|
|
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
foreach ($tempResult->getErrors() as $error) { |
63
|
|
|
$result->addError( |
64
|
|
|
$error->getMessage(), |
65
|
|
|
[$attribute . '.' . $nested, ...$error->getValuePath()], |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
12 |
|
$tempResult = $ruleSet->validate( |
70
|
12 |
|
$data->getAttributeValue($attribute), |
71
|
12 |
|
$context->withAttribute($attribute) |
72
|
|
|
); |
73
|
|
|
|
74
|
12 |
|
foreach ($tempResult->getErrors() as $error) { |
75
|
3 |
|
$result->addError($error->getMessage(), [$attribute, ...$error->getValuePath()]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
12 |
|
if ($data instanceof PostValidationHookInterface) { |
81
|
|
|
$data->processValidationResult($result); |
82
|
|
|
} |
83
|
|
|
|
84
|
12 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function withFormatter(?FormatterInterface $formatter): self |
88
|
|
|
{ |
89
|
|
|
$new = clone $this; |
90
|
|
|
$new->formatter = $formatter; |
91
|
|
|
return $new; |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
private function normalizeDataSet($data): DataSetInterface |
95
|
|
|
{ |
96
|
12 |
|
if ($data instanceof DataSetInterface) { |
97
|
5 |
|
return $data; |
98
|
|
|
} |
99
|
|
|
|
100
|
7 |
|
if (is_object($data) || is_array($data)) { |
101
|
1 |
|
return new ArrayDataSet((array)$data); |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
return new ScalarDataSet($data); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|