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\AfterInitAttributeEventInterface; |
11
|
|
|
use Yiisoft\Validator\Helper\RulesNormalizer; |
12
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
13
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
14
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
15
|
|
|
use Yiisoft\Validator\RuleInterface; |
16
|
|
|
use Yiisoft\Validator\RulesDumper; |
17
|
|
|
use Yiisoft\Validator\RuleWithOptionsInterface; |
18
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
19
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
20
|
|
|
use Yiisoft\Validator\WhenInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Allows to combine and validate multiple rules. |
24
|
|
|
* |
25
|
|
|
* @psalm-import-type WhenType from WhenInterface |
26
|
|
|
*/ |
27
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
28
|
|
|
class Composite implements |
29
|
|
|
RuleWithOptionsInterface, |
30
|
|
|
SkipOnErrorInterface, |
31
|
|
|
WhenInterface, |
32
|
|
|
SkipOnEmptyInterface, |
33
|
4 |
|
AfterInitAttributeEventInterface |
34
|
|
|
{ |
35
|
|
|
use SkipOnEmptyTrait; |
36
|
|
|
use SkipOnErrorTrait; |
37
|
|
|
use WhenTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var iterable<int, RuleInterface> |
41
|
|
|
*/ |
42
|
|
|
private iterable $rules; |
43
|
|
|
|
44
|
|
|
private ?RulesDumper $rulesDumper = null; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
/** |
48
|
|
|
* @param iterable<Closure|RuleInterface> |
49
|
4 |
|
*/ |
50
|
|
|
iterable $rules = [], |
51
|
|
|
|
|
|
|
|
52
|
1 |
|
/** |
53
|
|
|
* @var bool|callable|null |
54
|
1 |
|
*/ |
55
|
|
|
private $skipOnEmpty = null, |
56
|
|
|
private bool $skipOnError = false, |
57
|
3 |
|
/** |
58
|
|
|
* @var WhenType |
59
|
|
|
*/ |
60
|
|
|
private Closure|null $when = null, |
61
|
|
|
) { |
62
|
|
|
$this->rules = RulesNormalizer::normalizeList($rules); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function getName(): string |
66
|
3 |
|
{ |
67
|
3 |
|
return 'composite'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
#[ArrayShape([ |
71
|
|
|
'skipOnEmpty' => 'bool', |
72
|
|
|
'skipOnError' => 'bool', |
73
|
|
|
'rules' => 'array', |
74
|
2 |
|
])] |
75
|
|
|
public function getOptions(): array |
76
|
2 |
|
{ |
77
|
|
|
return [ |
78
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
79
|
3 |
|
'skipOnError' => $this->skipOnError, |
80
|
|
|
'rules' => $this->getRulesDumper()->asArray($this->rules), |
81
|
3 |
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return iterable<int, RuleInterface> |
86
|
|
|
*/ |
87
|
|
|
public function getRules(): iterable |
88
|
|
|
{ |
89
|
|
|
return $this->rules; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getHandlerClassName(): string |
93
|
|
|
{ |
94
|
|
|
return CompositeHandler::class; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function afterInitAttribute(object $object): void |
98
|
|
|
{ |
99
|
|
|
foreach ($this->rules as $rule) { |
100
|
|
|
if ($rule instanceof AfterInitAttributeEventInterface) { |
101
|
|
|
$rule->afterInitAttribute($object); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getRulesDumper(): RulesDumper |
107
|
|
|
{ |
108
|
|
|
if ($this->rulesDumper === null) { |
109
|
|
|
$this->rulesDumper = new RulesDumper(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->rulesDumper; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|