|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
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\SerializableRuleInterface; |
|
16
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
17
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
18
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
19
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Allows to combine and validate multiple rules. |
|
23
|
|
|
*/ |
|
24
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
25
|
|
|
class Composite implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use SkipOnEmptyTrait; |
|
28
|
|
|
use SkipOnErrorTrait; |
|
29
|
|
|
use WhenTrait; |
|
30
|
|
|
|
|
31
|
4 |
|
public function __construct( |
|
32
|
|
|
/** |
|
33
|
|
|
* @var iterable<RuleInterface> |
|
34
|
|
|
*/ |
|
35
|
|
|
private iterable $rules = [], |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool|callable|null |
|
39
|
|
|
*/ |
|
40
|
|
|
private $skipOnEmpty = null, |
|
41
|
|
|
private bool $skipOnError = false, |
|
42
|
|
|
/** |
|
43
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
44
|
|
|
*/ |
|
45
|
|
|
private ?Closure $when = null, |
|
46
|
|
|
) { |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function getName(): string |
|
50
|
|
|
{ |
|
51
|
1 |
|
return 'composite'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
#[ArrayShape([ |
|
55
|
|
|
'skipOnEmpty' => 'bool', |
|
56
|
|
|
'skipOnError' => 'bool', |
|
57
|
|
|
'rules' => 'array', |
|
58
|
|
|
])] |
|
59
|
|
|
public function getOptions(): array |
|
60
|
|
|
{ |
|
61
|
2 |
|
$arrayOfRules = []; |
|
62
|
2 |
|
foreach ($this->getRules() as $rule) { |
|
63
|
2 |
|
if (!$rule instanceof RuleInterface) { |
|
64
|
1 |
|
throw new InvalidArgumentException('A rule must be an instance of RuleInterface.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$nameArray = [$rule->getName()]; |
|
68
|
|
|
|
|
69
|
2 |
|
if ($rule instanceof SerializableRuleInterface) { |
|
70
|
2 |
|
$arrayOfRules[] = array_merge($nameArray, $rule->getOptions()); |
|
71
|
|
|
} else { |
|
72
|
|
|
$arrayOfRules[] = $nameArray; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
1 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
78
|
1 |
|
'skipOnError' => $this->skipOnError, |
|
79
|
|
|
'rules' => $arrayOfRules, |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]> |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function getRules(): iterable |
|
87
|
|
|
{ |
|
88
|
4 |
|
return $this->rules; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
public function getHandlerClassName(): string |
|
92
|
|
|
{ |
|
93
|
3 |
|
return CompositeHandler::class; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|