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