1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Traversable; |
9
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
10
|
|
|
use Yiisoft\Validator\DataSetInterface; |
11
|
|
|
use Yiisoft\Validator\Result; |
12
|
|
|
use Yiisoft\Validator\Rule; |
13
|
|
|
use Yiisoft\Validator\Rules; |
14
|
|
|
use function is_array; |
15
|
|
|
use function is_object; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Nested rule 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 with Nested rule we can configure it like this: |
32
|
|
|
* |
33
|
|
|
* ```php |
34
|
|
|
* $rule = new Nested([ |
35
|
|
|
* 'author.age' => [ |
36
|
|
|
* (new Number())->min(20), |
37
|
|
|
* ], |
38
|
|
|
* 'author.name' => [ |
39
|
|
|
* (new HasLength())->min(3), |
40
|
|
|
* ], |
41
|
|
|
* ]); |
42
|
|
|
* ``` |
43
|
|
|
*/ |
44
|
|
|
class Nested extends Rule |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var Rule[][] |
48
|
|
|
*/ |
49
|
|
|
private iterable $rules; |
50
|
|
|
|
51
|
|
|
private bool $errorWhenPropertyPathIsNotFound = false; |
52
|
|
|
private string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.'; |
53
|
|
|
|
54
|
11 |
|
public function __construct(iterable $rules) |
55
|
|
|
{ |
56
|
11 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
57
|
11 |
|
if (empty($rules)) { |
58
|
1 |
|
throw new InvalidArgumentException('Rules should not be empty.'); |
59
|
|
|
} |
60
|
10 |
|
if ($this->checkRules($rules)) { |
|
|
|
|
61
|
1 |
|
throw new InvalidArgumentException(sprintf( |
62
|
1 |
|
'Each rule should be an instance of %s.', |
63
|
1 |
|
Rule::class |
64
|
|
|
)); |
65
|
|
|
} |
66
|
9 |
|
$this->rules = $rules; |
|
|
|
|
67
|
9 |
|
} |
68
|
|
|
|
69
|
8 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
70
|
|
|
{ |
71
|
8 |
|
$result = new Result(); |
72
|
8 |
|
if (!is_object($value) && !is_array($value)) { |
73
|
1 |
|
$result->addError(sprintf( |
74
|
1 |
|
'Value should be an array or an object. %s given.', |
75
|
1 |
|
gettype($value) |
76
|
|
|
)); |
77
|
1 |
|
return $result; |
78
|
|
|
} |
79
|
7 |
|
$value = (array) $value; |
80
|
|
|
|
81
|
7 |
|
foreach ($this->rules as $valuePath => $rules) { |
82
|
7 |
|
$rulesSet = is_array($rules) ? $rules : [$rules]; |
83
|
7 |
|
if ($this->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) { |
84
|
2 |
|
$result->addError( |
85
|
2 |
|
$this->formatMessage( |
86
|
2 |
|
$this->propertyPathIsNotFoundMessage, |
87
|
|
|
[ |
88
|
2 |
|
'path' => $valuePath, |
89
|
|
|
] |
90
|
|
|
) |
91
|
|
|
); |
92
|
2 |
|
continue; |
93
|
|
|
} |
94
|
5 |
|
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath); |
95
|
5 |
|
$aggregateRule = new Rules($rulesSet); |
96
|
5 |
|
$itemResult = $aggregateRule->validate($validatedValue); |
97
|
5 |
|
if ($itemResult->isValid() === false) { |
98
|
3 |
|
foreach ($itemResult->getErrors() as $error) { |
99
|
3 |
|
$result->addError($error); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param bool $value If absence of nested property should be considered an error. Default is `false`. |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
2 |
|
public function errorWhenPropertyPathIsNotFound(bool $value): self |
113
|
|
|
{ |
114
|
2 |
|
$new = clone $this; |
115
|
2 |
|
$new->errorWhenPropertyPathIsNotFound = $value; |
116
|
2 |
|
return $new; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $message A message to use when nested property is absent. |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
1 |
|
public function propertyPathIsNotFoundMessage(string $message): self |
125
|
|
|
{ |
126
|
1 |
|
$new = clone $this; |
127
|
1 |
|
$new->propertyPathIsNotFoundMessage = $message; |
128
|
1 |
|
return $new; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getOptions(): array |
132
|
|
|
{ |
133
|
|
|
return $this->rules->asArray(); |
134
|
|
|
} |
135
|
|
|
|
136
|
10 |
|
private function checkRules(array $rules): bool |
137
|
|
|
{ |
138
|
10 |
|
return array_reduce( |
139
|
10 |
|
$rules, |
140
|
10 |
|
fn (bool $carry, $rule) => $carry || is_array($rule) ? $this->checkRules($rule) : !$rule instanceof Rule, |
141
|
10 |
|
false |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|