1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
9
|
|
|
use Yiisoft\Validator\Formatter; |
10
|
|
|
use Yiisoft\Validator\FormatterInterface; |
11
|
|
|
use Yiisoft\Validator\Result; |
12
|
|
|
use Yiisoft\Validator\ValidationContext; |
13
|
|
|
|
14
|
|
|
use function is_array; |
15
|
|
|
use function is_object; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Can be used for validation of nested structures. |
19
|
|
|
* |
20
|
|
|
* For example, we have an inbound request with the following structure: |
21
|
|
|
* |
22
|
|
|
* ```php |
23
|
|
|
* $request = [ |
24
|
|
|
* 'author' => [ |
25
|
|
|
* 'name' => 'Dmitry', |
26
|
|
|
* 'age' => 18, |
27
|
|
|
* ], |
28
|
|
|
* ]; |
29
|
|
|
* ``` |
30
|
|
|
* |
31
|
|
|
* So to make validation we can configure it like this: |
32
|
|
|
* |
33
|
|
|
* ```php |
34
|
|
|
* $rule = new Nested([ |
35
|
|
|
* 'author' => new Nested([ |
36
|
|
|
* 'name' => [new HasLength(min: 3)], |
37
|
|
|
* 'age' => [new Number(min: 18)], |
38
|
|
|
* )]; |
39
|
|
|
* ]); |
40
|
|
|
* ``` |
41
|
|
|
*/ |
42
|
|
|
final class NestedHandler implements RuleHandlerInterface |
43
|
|
|
{ |
44
|
|
|
private FormatterInterface $formatter; |
45
|
|
|
|
46
|
19 |
|
public function __construct(?FormatterInterface $formatter = null) |
47
|
|
|
{ |
48
|
19 |
|
$this->formatter = $formatter ?? new Formatter(); |
49
|
|
|
} |
50
|
|
|
|
51
|
19 |
|
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
52
|
|
|
{ |
53
|
19 |
|
if (!$rule instanceof Nested) { |
54
|
1 |
|
throw new UnexpectedRuleException(Nested::class, $rule); |
55
|
|
|
} |
56
|
|
|
|
57
|
18 |
|
$compoundResult = new Result(); |
58
|
18 |
|
if (!is_object($value) && !is_array($value)) { |
59
|
2 |
|
$message = sprintf('Value should be an array or an object. %s given.', gettype($value)); |
60
|
2 |
|
$formattedMessage = $this->formatter->format( |
61
|
|
|
$message, |
62
|
2 |
|
['attribute' => $context?->getAttribute(), 'value' => $value] |
63
|
|
|
); |
64
|
2 |
|
$compoundResult->addError($formattedMessage); |
65
|
|
|
|
66
|
2 |
|
return $compoundResult; |
67
|
|
|
} |
68
|
|
|
|
69
|
16 |
|
$value = (array)$value; |
70
|
|
|
|
71
|
16 |
|
$results = []; |
72
|
16 |
|
foreach ($rule->getRules() as $valuePath => $rules) { |
73
|
16 |
|
$result = new Result(); |
74
|
|
|
|
75
|
16 |
|
if ($rule->isErrorWhenPropertyPathIsNotFound() && !ArrayHelper::pathExists($value, $valuePath)) { |
76
|
3 |
|
$formattedMessage = $this->formatter->format( |
77
|
3 |
|
$rule->getPropertyPathIsNotFoundMessage(), |
78
|
3 |
|
['path' => $valuePath, 'attribute' => $context?->getAttribute(), 'value' => $value] |
79
|
|
|
); |
80
|
3 |
|
$compoundResult->addError($formattedMessage, [$valuePath]); |
81
|
|
|
|
82
|
3 |
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
13 |
|
$rules = is_array($rules) ? $rules : [$rules]; |
86
|
13 |
|
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath); |
87
|
|
|
|
88
|
13 |
|
$itemResult = $context?->getValidator()->validate($validatedValue, $rules); |
89
|
|
|
|
90
|
13 |
|
if ($itemResult->isValid()) { |
91
|
3 |
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
foreach ($itemResult->getErrors() as $error) { |
95
|
10 |
|
$errorValuePath = is_int($valuePath) ? [$valuePath] : explode('.', $valuePath); |
96
|
10 |
|
if (!empty($error->getValuePath())) { |
97
|
4 |
|
array_push($errorValuePath, ...$error->getValuePath()); |
98
|
|
|
} |
99
|
10 |
|
$result->addError($error->getMessage(), $errorValuePath); |
100
|
|
|
} |
101
|
10 |
|
$results[] = $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
16 |
|
foreach ($results as $result) { |
105
|
10 |
|
foreach ($result->getErrors() as $error) { |
106
|
10 |
|
$compoundResult->addError($error->getMessage(), $error->getValuePath()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
16 |
|
return $compoundResult; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|