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\Rule\Trait\BeforeValidationTrait; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
12
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
13
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
16
|
|
|
|
17
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
18
|
|
|
final class Subset implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface |
19
|
|
|
{ |
20
|
|
|
use BeforeValidationTrait; |
21
|
|
|
use RuleNameTrait; |
22
|
|
|
use SkipOnEmptyTrait; |
23
|
|
|
|
24
|
2 |
|
public function __construct( |
25
|
|
|
private iterable $values, |
26
|
|
|
/** |
27
|
|
|
* @var bool whether the comparison is strict (both type and value must be the same) |
28
|
|
|
*/ |
29
|
|
|
private bool $strict = false, |
30
|
|
|
private string $iterableMessage = 'Value must be iterable.', |
31
|
|
|
private string $subsetMessage = 'Values must be ones of {values}.', |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool|callable|null |
35
|
|
|
*/ |
36
|
|
|
private $skipOnEmpty = null, |
37
|
|
|
private bool $skipOnError = false, |
38
|
|
|
/** |
39
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
40
|
|
|
*/ |
41
|
|
|
private ?Closure $when = null, |
42
|
|
|
) { |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return iterable |
47
|
|
|
*/ |
48
|
9 |
|
public function getValues(): iterable |
49
|
|
|
{ |
50
|
9 |
|
return $this->values; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
9 |
|
public function isStrict(): bool |
57
|
|
|
{ |
58
|
9 |
|
return $this->strict; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getIterableMessage(): string |
65
|
|
|
{ |
66
|
|
|
return $this->iterableMessage; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
3 |
|
public function getSubsetMessage(): string |
73
|
|
|
{ |
74
|
3 |
|
return $this->subsetMessage; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function getOptions(): array |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
1 |
|
'values' => $this->values, |
81
|
1 |
|
'strict' => $this->strict, |
82
|
|
|
'iterableMessage' => [ |
83
|
1 |
|
'message' => $this->iterableMessage, |
84
|
|
|
], |
85
|
|
|
'subsetMessage' => [ |
86
|
1 |
|
'message' => $this->subsetMessage, |
87
|
|
|
], |
88
|
1 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
89
|
1 |
|
'skipOnError' => $this->skipOnError, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getHandlerClassName(): string |
94
|
|
|
{ |
95
|
1 |
|
return SubsetHandler::class; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|