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\ParametrizedRuleInterface; |
11
|
|
|
use Yiisoft\Validator\Result; |
12
|
|
|
use Yiisoft\Validator\Rule; |
13
|
|
|
use Yiisoft\Validator\RuleInterface; |
14
|
|
|
use Yiisoft\Validator\Rules; |
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
16
|
|
|
use function is_array; |
17
|
|
|
use function is_object; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Nested rule can be used for validation of nested structures. |
21
|
|
|
* |
22
|
|
|
* For example we have an inbound request with the following structure: |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* $request = [ |
26
|
|
|
* 'author' => [ |
27
|
|
|
* 'name' => 'Dmitry', |
28
|
|
|
* 'age' => 18, |
29
|
|
|
* ], |
30
|
|
|
* ]; |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* So to make validation with Nested rule we can configure it like this: |
34
|
|
|
* |
35
|
|
|
* ```php |
36
|
|
|
* $rule = new Nested([ |
37
|
|
|
* 'author.age' => [ |
38
|
|
|
* (new Number())->min(20), |
39
|
|
|
* ], |
40
|
|
|
* 'author.name' => [ |
41
|
|
|
* (new HasLength())->min(3), |
42
|
|
|
* ], |
43
|
|
|
* ]); |
44
|
|
|
* ``` |
45
|
|
|
*/ |
46
|
|
|
class Nested extends Rule |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var Rule[][] |
50
|
|
|
*/ |
51
|
|
|
private iterable $rules; |
52
|
|
|
|
53
|
|
|
private bool $errorWhenPropertyPathIsNotFound = false; |
54
|
|
|
private string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.'; |
55
|
|
|
|
56
|
13 |
|
public static function rule(iterable $rules): self |
57
|
|
|
{ |
58
|
13 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
59
|
13 |
|
if (empty($rules)) { |
60
|
1 |
|
throw new InvalidArgumentException('Rules should not be empty.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
$rule = new self(); |
64
|
12 |
|
if ($rule->checkRules($rules)) { |
|
|
|
|
65
|
1 |
|
throw new InvalidArgumentException(sprintf( |
66
|
1 |
|
'Each rule should be an instance of %s.', |
67
|
1 |
|
RuleInterface::class |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
$rule->rules = $rules; |
|
|
|
|
72
|
|
|
|
73
|
11 |
|
return $rule; |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
protected function validateValue($value, ValidationContext $context = null): Result |
77
|
|
|
{ |
78
|
8 |
|
$result = new Result(); |
79
|
8 |
|
if (!is_object($value) && !is_array($value)) { |
80
|
1 |
|
$result->addError(sprintf( |
81
|
1 |
|
'Value should be an array or an object. %s given.', |
82
|
1 |
|
gettype($value) |
83
|
|
|
)); |
84
|
1 |
|
return $result; |
85
|
|
|
} |
86
|
7 |
|
$value = (array) $value; |
87
|
|
|
|
88
|
7 |
|
foreach ($this->rules as $valuePath => $rules) { |
89
|
7 |
|
$rulesSet = is_array($rules) ? $rules : [$rules]; |
90
|
7 |
|
if ($this->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) { |
91
|
2 |
|
$result->addError( |
92
|
2 |
|
$this->formatMessage( |
93
|
2 |
|
$this->propertyPathIsNotFoundMessage, |
94
|
|
|
[ |
95
|
2 |
|
'path' => $valuePath, |
96
|
|
|
] |
97
|
|
|
) |
98
|
|
|
); |
99
|
2 |
|
continue; |
100
|
|
|
} |
101
|
5 |
|
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath); |
102
|
5 |
|
$aggregateRule = new Rules($rulesSet); |
103
|
5 |
|
$itemResult = $aggregateRule->validate($validatedValue); |
104
|
5 |
|
if ($itemResult->isValid() === false) { |
105
|
3 |
|
foreach ($itemResult->getErrors() as $error) { |
106
|
3 |
|
$result->addError($error); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool $value If absence of nested property should be considered an error. Default is `false`. |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
2 |
|
public function errorWhenPropertyPathIsNotFound(bool $value): self |
120
|
|
|
{ |
121
|
2 |
|
$new = clone $this; |
122
|
2 |
|
$new->errorWhenPropertyPathIsNotFound = $value; |
123
|
2 |
|
return $new; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $message A message to use when nested property is absent. |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
1 |
|
public function propertyPathIsNotFoundMessage(string $message): self |
132
|
|
|
{ |
133
|
1 |
|
$new = clone $this; |
134
|
1 |
|
$new->propertyPathIsNotFoundMessage = $message; |
135
|
1 |
|
return $new; |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public function getOptions(): array |
139
|
|
|
{ |
140
|
2 |
|
return $this->fetchOptions($this->rules); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
12 |
|
private function checkRules(array $rules): bool |
144
|
|
|
{ |
145
|
12 |
|
return array_reduce( |
146
|
12 |
|
$rules, |
147
|
12 |
|
fn (bool $carry, $rule) => $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface), |
148
|
12 |
|
false |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
private function fetchOptions(array $rules): array |
153
|
|
|
{ |
154
|
2 |
|
$result = []; |
155
|
2 |
|
foreach ($rules as $attribute => $rule) { |
156
|
2 |
|
if (is_array($rule)) { |
157
|
1 |
|
$result[$attribute] = $this->fetchOptions($rule); |
158
|
2 |
|
} elseif ($rule instanceof ParametrizedRuleInterface) { |
159
|
2 |
|
$result[$attribute] = $rule->getOptions(); |
160
|
|
|
} elseif ($rule instanceof RuleInterface) { |
161
|
|
|
// Just skip the rule that doesn't support parametrizing |
162
|
|
|
} else { |
163
|
|
|
throw new \InvalidArgumentException(sprintf( |
164
|
|
|
'Rules should be an array of rules that implements %s.', |
165
|
|
|
ParametrizedRuleInterface::class, |
166
|
|
|
)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
return $result; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|