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