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
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Nested rule needed for validation nested structures. |
17
|
|
|
* |
18
|
|
|
* For example we have an inbound request with the following structure: |
19
|
|
|
* $request = [ |
20
|
|
|
* 'author' => [ |
21
|
|
|
* 'name' => 'Dmitry', |
22
|
|
|
* 'age' => 18, |
23
|
|
|
* ], |
24
|
|
|
* ]; |
25
|
|
|
* |
26
|
|
|
* So to make validation with Nested rule we can configure it like this: |
27
|
|
|
* $rule = new Nested([ |
28
|
|
|
* 'author.age' => [ |
29
|
|
|
* (new Number())->min(20), |
30
|
|
|
* ], |
31
|
|
|
* 'author.name' => [ |
32
|
|
|
* (new HasLength())->min(3), |
33
|
|
|
* ], |
34
|
|
|
* ]); |
35
|
|
|
*/ |
36
|
|
|
class Nested extends Rule |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Rule[][] |
40
|
|
|
*/ |
41
|
|
|
private iterable $rules; |
42
|
|
|
|
43
|
|
|
private bool $errorWhenPropertyPathIsNotFound = false; |
44
|
|
|
private string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.'; |
45
|
|
|
|
46
|
11 |
|
public function __construct(iterable $rules) |
47
|
|
|
{ |
48
|
11 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
49
|
11 |
|
if (empty($rules)) { |
50
|
1 |
|
throw new InvalidArgumentException('Rules should not be empty.'); |
51
|
|
|
} |
52
|
10 |
|
if ($this->checkRules($rules)) { |
|
|
|
|
53
|
1 |
|
throw new InvalidArgumentException(sprintf( |
54
|
1 |
|
'Each rule should be instance of %s.', |
55
|
1 |
|
Rule::class |
56
|
|
|
)); |
57
|
|
|
} |
58
|
9 |
|
$this->rules = $rules; |
|
|
|
|
59
|
9 |
|
} |
60
|
|
|
|
61
|
8 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
62
|
|
|
{ |
63
|
8 |
|
$result = new Result(); |
64
|
8 |
|
if (!is_object($value) && !is_array($value)) { |
65
|
1 |
|
$result->addError(sprintf( |
66
|
1 |
|
'Value should be an array or an object. %s given', |
67
|
1 |
|
gettype($value) |
68
|
|
|
)); |
69
|
1 |
|
return $result; |
70
|
|
|
} |
71
|
7 |
|
$value = (array) $value; |
72
|
|
|
|
73
|
7 |
|
foreach ($this->rules as $valuePath => $rules) { |
74
|
7 |
|
$rulesSet = is_array($rules) ? $rules : [$rules]; |
75
|
7 |
|
if ($this->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) { |
76
|
2 |
|
$result->addError( |
77
|
2 |
|
$this->formatMessage( |
78
|
2 |
|
$this->propertyPathIsNotFoundMessage, |
79
|
|
|
[ |
80
|
2 |
|
'path' => $valuePath, |
81
|
|
|
] |
82
|
|
|
) |
83
|
|
|
); |
84
|
2 |
|
continue; |
85
|
|
|
} |
86
|
5 |
|
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath); |
87
|
5 |
|
$aggregateRule = new Rules($rulesSet); |
88
|
5 |
|
$itemResult = $aggregateRule->validate($validatedValue); |
89
|
5 |
|
if ($itemResult->isValid() === false) { |
90
|
3 |
|
foreach ($itemResult->getErrors() as $error) { |
91
|
3 |
|
$result->addError($error); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function errorWhenPropertyPathIsNotFound(bool $value): self |
100
|
|
|
{ |
101
|
2 |
|
$new = clone $this; |
102
|
2 |
|
$new->errorWhenPropertyPathIsNotFound = $value; |
103
|
2 |
|
return $new; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function propertyPathIsNotFoundMessage(string $message): self |
107
|
|
|
{ |
108
|
1 |
|
$new = clone $this; |
109
|
1 |
|
$new->propertyPathIsNotFoundMessage = $message; |
110
|
1 |
|
return $new; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getOptions(): array |
114
|
|
|
{ |
115
|
|
|
return $this->rules->asArray(); |
116
|
|
|
} |
117
|
|
|
|
118
|
10 |
|
private function checkRules(array $rules): bool |
119
|
|
|
{ |
120
|
10 |
|
return array_reduce( |
121
|
10 |
|
$rules, |
122
|
10 |
|
fn (bool $carry, $rule) => $carry || is_array($rule) ? $this->checkRules($rule) : !$rule instanceof Rule, |
123
|
10 |
|
false |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|