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 JetBrains\PhpStorm\ArrayShape; |
11
|
|
|
use Traversable; |
12
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
13
|
|
|
use Yiisoft\Validator\BeforeValidationInterface; |
14
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
15
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
16
|
|
|
use Yiisoft\Validator\RuleInterface; |
17
|
|
|
use Yiisoft\Validator\RulesDumper; |
18
|
|
|
|
19
|
|
|
use Yiisoft\Validator\ValidationContext; |
20
|
|
|
|
21
|
|
|
use function is_array; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Can be used for validation of nested structures. |
25
|
|
|
*/ |
26
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
27
|
|
|
final class Nested implements SerializableRuleInterface, BeforeValidationInterface |
28
|
|
|
{ |
29
|
|
|
use BeforeValidationTrait; |
30
|
|
|
use RuleNameTrait; |
31
|
|
|
|
32
|
4 |
|
public function __construct( |
33
|
|
|
/** |
34
|
|
|
* @var iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]> |
35
|
|
|
*/ |
36
|
|
|
private iterable $rules = [], |
37
|
|
|
private bool $requirePropertyPath = false, |
38
|
|
|
private string $noPropertyPathMessage = 'Property path "{path}" is not found.', |
39
|
|
|
private bool $skipOnEmpty = false, |
40
|
|
|
private bool $skipOnError = false, |
41
|
|
|
/** |
42
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
43
|
|
|
*/ |
44
|
|
|
private ?Closure $when = null, |
45
|
|
|
) { |
46
|
4 |
|
$rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; |
47
|
4 |
|
if (empty($rules)) { |
48
|
1 |
|
throw new InvalidArgumentException('Rules must not be empty.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
if ($this->checkRules($rules)) { |
|
|
|
|
52
|
1 |
|
$message = sprintf('Each rule should be an instance of %s.', RuleInterface::class); |
53
|
1 |
|
throw new InvalidArgumentException($message); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$this->rules = $rules; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]> |
61
|
|
|
*/ |
62
|
16 |
|
public function getRules(): iterable |
63
|
|
|
{ |
64
|
16 |
|
return $this->rules; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
20 |
|
public function getRequirePropertyPath(): bool |
71
|
|
|
{ |
72
|
20 |
|
return $this->requirePropertyPath; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
7 |
|
public function getNoPropertyPathMessage(): string |
79
|
|
|
{ |
80
|
7 |
|
return $this->noPropertyPathMessage; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
private function checkRules(array $rules): bool |
84
|
|
|
{ |
85
|
3 |
|
return array_reduce( |
86
|
|
|
$rules, |
87
|
3 |
|
function (bool $carry, $rule) { |
88
|
3 |
|
return $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface); |
89
|
|
|
}, |
90
|
|
|
false |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
#[ArrayShape([ |
95
|
|
|
'requirePropertyPath' => 'bool', |
96
|
|
|
'noPropertyPathMessage' => 'array', |
97
|
|
|
'skipOnEmpty' => 'bool', |
98
|
|
|
'skipOnError' => 'bool', |
99
|
|
|
'rules' => 'array', |
100
|
|
|
])] |
101
|
|
|
public function getOptions(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
4 |
|
'requirePropertyPath' => $this->getRequirePropertyPath(), |
105
|
|
|
'noPropertyPathMessage' => [ |
106
|
4 |
|
'message' => $this->getNoPropertyPathMessage(), |
107
|
|
|
], |
108
|
4 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
109
|
4 |
|
'skipOnError' => $this->skipOnError, |
110
|
4 |
|
'rules' => (new RulesDumper())->asArray($this->rules), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
6 |
|
public function getHandlerClassName(): string |
115
|
|
|
{ |
116
|
6 |
|
return NestedHandler::class; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|