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