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