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