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