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