|
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
|
|
|
protected iterable $rules = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var bool|callable|null |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $skipOnEmpty = null; |
|
48
|
|
|
|
|
49
|
4 |
|
protected bool $skipOnError = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
1 |
|
* @psalm-var WhenType |
|
53
|
|
|
*/ |
|
54
|
1 |
|
protected Closure|null $when = null; |
|
55
|
|
|
|
|
56
|
|
|
private ?RulesDumper $rulesDumper = null; |
|
57
|
3 |
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param iterable<Closure|RuleInterface> $rules |
|
60
|
|
|
* |
|
61
|
|
|
* @psalm-param WhenType $when |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct( |
|
64
|
|
|
iterable $rules = [], |
|
65
|
3 |
|
bool|callable|null $skipOnEmpty = null, |
|
66
|
3 |
|
bool $skipOnError = false, |
|
67
|
3 |
|
Closure|null $when = null, |
|
68
|
|
|
) { |
|
69
|
|
|
$this->rules = RulesNormalizer::normalizeList($rules); |
|
70
|
|
|
$this->skipOnEmpty = $skipOnEmpty; |
|
71
|
|
|
$this->skipOnError = $skipOnError; |
|
72
|
|
|
$this->when = $when; |
|
73
|
|
|
} |
|
74
|
2 |
|
|
|
75
|
|
|
public function getName(): string |
|
76
|
2 |
|
{ |
|
77
|
|
|
return 'composite'; |
|
78
|
|
|
} |
|
79
|
3 |
|
|
|
80
|
|
|
#[ArrayShape([ |
|
81
|
3 |
|
'skipOnEmpty' => 'bool', |
|
82
|
|
|
'skipOnError' => 'bool', |
|
83
|
|
|
'rules' => 'array', |
|
84
|
|
|
])] |
|
85
|
|
|
public function getOptions(): array |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
89
|
|
|
'skipOnError' => $this->skipOnError, |
|
90
|
|
|
'rules' => $this->dumpRulesAsArray(), |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return iterable<int, RuleInterface> |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getRules(): iterable |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->rules; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
final public function getHandlerClassName(): string |
|
103
|
|
|
{ |
|
104
|
|
|
return CompositeHandler::class; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function afterInitAttribute(object $object): void |
|
108
|
|
|
{ |
|
109
|
|
|
foreach ($this->getRules() as $rule) { |
|
110
|
|
|
if ($rule instanceof AfterInitAttributeEventInterface) { |
|
111
|
|
|
$rule->afterInitAttribute($object); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
final protected function dumpRulesAsArray(): array |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getRulesDumper()->asArray($this->getRules()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function getRulesDumper(): RulesDumper |
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->rulesDumper === null) { |
|
124
|
|
|
$this->rulesDumper = new RulesDumper(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->rulesDumper; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|