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\RuleSet; |
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 = Nested::rule([ |
37
|
|
|
* 'author' => Nested::rule([ |
38
|
|
|
* 'name' => [HasLength::rule()->min(3)], |
39
|
|
|
* 'age' => [new Number(min: 18)], |
40
|
|
|
* )]; |
41
|
|
|
* ]); |
42
|
|
|
* ``` |
43
|
|
|
*/ |
44
|
|
|
final 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
|
17 |
|
public static function rule(iterable $rules): self |
55
|
|
|
{ |
56
|
17 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
57
|
17 |
|
if (empty($rules)) { |
58
|
1 |
|
throw new InvalidArgumentException('Rules should not be empty.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
16 |
|
$rule = new self(); |
62
|
16 |
|
if ($rule->checkRules($rules)) { |
|
|
|
|
63
|
1 |
|
throw new InvalidArgumentException(sprintf( |
64
|
1 |
|
'Each rule should be an instance of %s.', |
65
|
|
|
RuleInterface::class |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
$rule->rules = $rules; |
|
|
|
|
70
|
|
|
|
71
|
15 |
|
return $rule; |
72
|
|
|
} |
73
|
|
|
|
74
|
11 |
|
protected function validateValue($value, ?ValidationContext $context = null): Result |
75
|
|
|
{ |
76
|
11 |
|
$result = new Result(); |
77
|
11 |
|
if (!is_object($value) && !is_array($value)) { |
78
|
1 |
|
$result->addError(sprintf( |
79
|
1 |
|
'Value should be an array or an object. %s given.', |
80
|
1 |
|
gettype($value) |
81
|
|
|
)); |
82
|
|
|
|
83
|
1 |
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
$value = (array) $value; |
87
|
|
|
|
88
|
10 |
|
foreach ($this->rules as $valuePath => $rules) { |
89
|
10 |
|
if ($this->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) { |
90
|
2 |
|
$message = $this->formatMessage($this->propertyPathIsNotFoundMessage, ['path' => $valuePath]); |
91
|
2 |
|
$result->addError($message); |
92
|
|
|
|
93
|
2 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
$rules = is_array($rules) ? $rules : [$rules]; |
97
|
8 |
|
$ruleSet = new RuleSet($rules); |
98
|
8 |
|
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath); |
99
|
8 |
|
$itemResult = $ruleSet->validate($validatedValue); |
100
|
8 |
|
if ($itemResult->isValid()) { |
101
|
3 |
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
foreach ($itemResult->getErrors() as $error) { |
105
|
6 |
|
$errorValuePath = is_int($valuePath) ? [$valuePath] : explode('.', $valuePath); |
106
|
6 |
|
if ($error->getValuePath()) { |
107
|
3 |
|
$errorValuePath = array_merge($errorValuePath, $error->getValuePath()); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
$result->addError($error->getMessage(), $errorValuePath); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
10 |
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool $value If absence of nested property should be considered an error. Default is `false`. |
119
|
|
|
* |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
2 |
|
public function errorWhenPropertyPathIsNotFound(bool $value): self |
123
|
|
|
{ |
124
|
2 |
|
$new = clone $this; |
125
|
2 |
|
$new->errorWhenPropertyPathIsNotFound = $value; |
126
|
2 |
|
return $new; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $message A message to use when nested property is absent. |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
1 |
|
public function propertyPathIsNotFoundMessage(string $message): self |
135
|
|
|
{ |
136
|
1 |
|
$new = clone $this; |
137
|
1 |
|
$new->propertyPathIsNotFoundMessage = $message; |
138
|
1 |
|
return $new; |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
public function getOptions(): array |
142
|
|
|
{ |
143
|
3 |
|
return $this->fetchOptions($this->rules); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
16 |
|
private function checkRules(array $rules): bool |
147
|
|
|
{ |
148
|
16 |
|
return array_reduce( |
149
|
|
|
$rules, |
150
|
16 |
|
fn (bool $carry, $rule) => $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface), |
151
|
|
|
false |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
private function fetchOptions(array $rules): array |
156
|
|
|
{ |
157
|
3 |
|
$result = []; |
158
|
3 |
|
foreach ($rules as $attribute => $rule) { |
159
|
3 |
|
if (is_array($rule)) { |
160
|
2 |
|
$result[$attribute] = $this->fetchOptions($rule); |
161
|
3 |
|
} elseif ($rule instanceof ParametrizedRuleInterface) { |
162
|
3 |
|
$result[$attribute] = $rule->getOptions(); |
163
|
|
|
} elseif ($rule instanceof RuleInterface) { |
164
|
|
|
// Just skip the rule that doesn't support parametrizing |
165
|
|
|
} else { |
166
|
|
|
throw new \InvalidArgumentException(sprintf( |
167
|
|
|
'Rules should be an array of rules that implements %s.', |
168
|
|
|
ParametrizedRuleInterface::class, |
169
|
|
|
)); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
3 |
|
return $result; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|