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
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
22
|
|
|
final class Nested implements ParametrizedRuleInterface |
23
|
|
|
{ |
24
|
|
|
use HandlerClassNameTrait; |
25
|
|
|
use RuleNameTrait; |
26
|
|
|
|
27
|
3 |
|
public function __construct( |
28
|
|
|
/** |
29
|
|
|
* @var RuleInterface[][] |
30
|
|
|
*/ |
31
|
|
|
private iterable $rules = [], |
32
|
|
|
private bool $errorWhenPropertyPathIsNotFound = false, |
33
|
|
|
private string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.', |
34
|
|
|
private bool $skipOnEmpty = false, |
35
|
|
|
private bool $skipOnError = false, |
36
|
|
|
private ?Closure $when = null, |
37
|
|
|
) { |
38
|
3 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
39
|
3 |
|
if (empty($rules)) { |
40
|
1 |
|
throw new InvalidArgumentException('Rules must not be empty.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
if ($this->checkRules($rules)) { |
44
|
1 |
|
$message = sprintf('Each rule should be an instance of %s.', RuleInterface::class); |
45
|
1 |
|
throw new InvalidArgumentException($message); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$this->rules = $rules; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return iterable |
53
|
|
|
*/ |
54
|
10 |
|
public function getRules(): iterable |
55
|
|
|
{ |
56
|
10 |
|
return $this->rules; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
10 |
|
public function isErrorWhenPropertyPathIsNotFound(): bool |
63
|
|
|
{ |
64
|
10 |
|
return $this->errorWhenPropertyPathIsNotFound; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
2 |
|
public function getPropertyPathIsNotFoundMessage(): string |
71
|
|
|
{ |
72
|
2 |
|
return $this->propertyPathIsNotFoundMessage; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isSkipOnEmpty(): bool |
79
|
|
|
{ |
80
|
|
|
return $this->skipOnEmpty; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isSkipOnError(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->skipOnError; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Closure|null |
93
|
|
|
*/ |
94
|
|
|
public function getWhen(): ?Closure |
95
|
|
|
{ |
96
|
|
|
return $this->when; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
private function checkRules(array $rules): bool |
100
|
|
|
{ |
101
|
2 |
|
return array_reduce( |
102
|
|
|
$rules, |
103
|
2 |
|
function (bool $carry, $rule) { |
104
|
2 |
|
return $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface); |
105
|
|
|
}, |
106
|
|
|
false |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
public function getOptions(): array |
111
|
|
|
{ |
112
|
4 |
|
return (new RulesDumper())->asArray($this->rules); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|