1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Traversable; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
|
|
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
13
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
14
|
|
|
use Yiisoft\Validator\RuleInterface; |
15
|
|
|
use Yiisoft\Validator\RulesDumper; |
16
|
|
|
use function is_array; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Can be used for validation of nested structures. |
20
|
|
|
* |
21
|
|
|
* For example, we have an inbound request with the following structure: |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* $request = [ |
25
|
|
|
* 'author' => [ |
26
|
|
|
* 'name' => 'Dmitry', |
27
|
|
|
* 'age' => 18, |
28
|
|
|
* ], |
29
|
|
|
* ]; |
30
|
|
|
* ``` |
31
|
|
|
* |
32
|
|
|
* So to make validation we can configure it like this: |
33
|
|
|
* |
34
|
|
|
* ```php |
35
|
|
|
* $rule = new Nested([ |
36
|
|
|
* 'author' => new Nested([ |
37
|
|
|
* 'name' => [new HasLength(min: 3)], |
38
|
|
|
* 'age' => [new Number(min: 18)], |
39
|
|
|
* )]; |
40
|
|
|
* ]); |
41
|
|
|
* ``` |
42
|
|
|
*/ |
43
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
44
|
|
|
final class Nested implements ParametrizedRuleInterface |
45
|
|
|
{ |
46
|
|
|
use HandlerClassNameTrait; |
47
|
|
|
use RuleNameTrait; |
48
|
|
|
|
49
|
3 |
|
public function __construct( |
50
|
|
|
/** |
51
|
|
|
* @var RuleInterface[][] |
52
|
|
|
*/ |
53
|
|
|
public iterable $rules = [], |
54
|
|
|
public bool $errorWhenPropertyPathIsNotFound = false, |
55
|
|
|
public string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.', |
56
|
|
|
public bool $skipOnEmpty = false, |
57
|
|
|
public bool $skipOnError = false, |
58
|
|
|
public ?Closure $when = null, |
59
|
|
|
) { |
60
|
3 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
61
|
3 |
|
if (empty($rules)) { |
62
|
1 |
|
throw new InvalidArgumentException('Rules must not be empty.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
if ($this->checkRules($rules)) { |
66
|
1 |
|
$message = sprintf('Each rule should be an instance of %s.', RuleInterface::class); |
67
|
1 |
|
throw new InvalidArgumentException($message); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$this->rules = $rules; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
private function checkRules(array $rules): bool |
74
|
|
|
{ |
75
|
2 |
|
return array_reduce( |
76
|
|
|
$rules, |
77
|
2 |
|
function (bool $carry, $rule) { |
78
|
2 |
|
return $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface); |
79
|
|
|
}, |
80
|
|
|
false |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
public function getOptions(): array |
85
|
|
|
{ |
86
|
4 |
|
$dumper = new RulesDumper(); |
87
|
|
|
|
88
|
4 |
|
return $dumper->asArray($this->rules, false); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|