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\BeforeValidationInterface; |
10
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
12
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
13
|
|
|
use Yiisoft\Validator\RuleInterface; |
14
|
|
|
use Yiisoft\Validator\ValidationContext; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Validates that the value is a valid json. |
18
|
|
|
*/ |
19
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
20
|
|
|
class Composite implements SerializableRuleInterface, BeforeValidationInterface |
21
|
|
|
{ |
22
|
|
|
use BeforeValidationTrait; |
23
|
|
|
use RuleNameTrait; |
24
|
|
|
|
25
|
2 |
|
public function __construct( |
26
|
|
|
/** |
27
|
|
|
* @var iterable<RuleInterface> |
28
|
|
|
*/ |
29
|
|
|
private iterable $rules = [], |
30
|
|
|
private bool $skipOnEmpty = false, |
31
|
|
|
private bool $skipOnError = false, |
32
|
|
|
/** |
33
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
34
|
|
|
*/ |
35
|
|
|
private ?Closure $when = null, |
36
|
|
|
) { |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function getOptions(): array |
40
|
|
|
{ |
41
|
1 |
|
$arrayOfRules = []; |
42
|
1 |
|
foreach ($this->rules as $rule) { |
43
|
1 |
|
if ($rule instanceof SerializableRuleInterface) { |
44
|
1 |
|
$arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions()); |
45
|
|
|
} else { |
46
|
|
|
$arrayOfRules[] = [$rule->getName()]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
1 |
|
return $arrayOfRules; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return iterable<RuleInterface> |
54
|
|
|
*/ |
55
|
2 |
|
public function getRules(): iterable |
56
|
|
|
{ |
57
|
2 |
|
return $this->rules; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function getHandlerClassName(): string |
61
|
|
|
{ |
62
|
1 |
|
return CompositeHandler::class; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|