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