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