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