|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
10
|
|
|
use Yiisoft\Validator\PropagateOptionsInterface; |
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
|
14
|
|
|
use Yiisoft\Validator\RuleInterface; |
|
15
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
|
16
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
17
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
18
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
19
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Validates an array by checking each of its elements against a set of rules. |
|
23
|
|
|
*/ |
|
24
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
25
|
|
|
final class Each implements |
|
26
|
|
|
SerializableRuleInterface, |
|
27
|
|
|
SkipOnErrorInterface, |
|
28
|
|
|
WhenInterface, |
|
29
|
|
|
SkipOnEmptyInterface, |
|
30
|
|
|
PropagateOptionsInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use SkipOnEmptyTrait; |
|
33
|
|
|
use SkipOnErrorTrait; |
|
34
|
|
|
use WhenTrait; |
|
35
|
|
|
|
|
36
|
6 |
|
public function __construct( |
|
37
|
|
|
/** |
|
38
|
|
|
* @var iterable<RuleInterface> |
|
39
|
|
|
*/ |
|
40
|
|
|
private iterable $rules = [], |
|
41
|
|
|
private string $incorrectInputMessage = 'Value must be array or iterable.', |
|
42
|
|
|
private string $incorrectInputKeyMessage = 'Every iterable key must have an integer or a string type.', |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var bool|callable|null |
|
46
|
|
|
*/ |
|
47
|
|
|
private $skipOnEmpty = null, |
|
48
|
|
|
private bool $skipOnError = false, |
|
49
|
|
|
/** |
|
50
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
51
|
|
|
*/ |
|
52
|
|
|
private ?Closure $when = null, |
|
53
|
|
|
) { |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function getName(): string |
|
57
|
|
|
{ |
|
58
|
2 |
|
return 'each'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function propagateOptions(): void |
|
62
|
|
|
{ |
|
63
|
1 |
|
$rules = []; |
|
64
|
1 |
|
foreach ($this->rules as $rule) { |
|
65
|
1 |
|
if ($rule instanceof SkipOnEmptyInterface) { |
|
66
|
1 |
|
$rule = $rule->skipOnEmpty($this->skipOnEmpty); |
|
67
|
|
|
} |
|
68
|
1 |
|
if ($rule instanceof SkipOnErrorInterface) { |
|
69
|
1 |
|
$rule = $rule->skipOnError($this->skipOnError); |
|
70
|
|
|
} |
|
71
|
1 |
|
if ($rule instanceof WhenInterface) { |
|
72
|
1 |
|
$rule = $rule->when($this->when); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$rules[] = $rule; |
|
76
|
|
|
|
|
77
|
1 |
|
if ($rule instanceof PropagateOptionsInterface) { |
|
78
|
1 |
|
$rule->propagateOptions(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$this->rules = $rules; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> |
|
87
|
|
|
*/ |
|
88
|
11 |
|
public function getRules(): iterable |
|
89
|
|
|
{ |
|
90
|
11 |
|
return $this->rules; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
public function getIncorrectInputMessage(): string |
|
94
|
|
|
{ |
|
95
|
2 |
|
return $this->incorrectInputMessage; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function getIncorrectInputKeyMessage(): string |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->incorrectInputKeyMessage; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
#[ArrayShape([ |
|
104
|
|
|
'incorrectInputMessage' => 'array', |
|
105
|
|
|
'incorrectInputKeyMessage' => 'array', |
|
106
|
|
|
'skipOnEmpty' => 'bool', |
|
107
|
|
|
'skipOnError' => 'bool', |
|
108
|
|
|
'rules' => 'array', |
|
109
|
|
|
])] |
|
110
|
|
|
public function getOptions(): array |
|
111
|
|
|
{ |
|
112
|
2 |
|
$arrayOfRules = []; |
|
113
|
2 |
|
foreach ($this->rules as $rule) { |
|
114
|
2 |
|
if ($rule instanceof SerializableRuleInterface) { |
|
115
|
2 |
|
$arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions()); |
|
116
|
|
|
} else { |
|
117
|
|
|
$arrayOfRules[] = [$rule->getName()]; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return [ |
|
122
|
|
|
'incorrectInputMessage' => [ |
|
123
|
2 |
|
'message' => $this->incorrectInputMessage, |
|
124
|
|
|
], |
|
125
|
|
|
'incorrectInputKeyMessage' => [ |
|
126
|
2 |
|
'message' => $this->incorrectInputKeyMessage, |
|
127
|
|
|
], |
|
128
|
2 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
129
|
2 |
|
'skipOnError' => $this->skipOnError, |
|
130
|
|
|
'rules' => $arrayOfRules, |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
11 |
|
public function getHandlerClassName(): string |
|
135
|
|
|
{ |
|
136
|
11 |
|
return EachHandler::class; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|