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