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\ParametrizedRuleInterface; |
11
|
|
|
use Yiisoft\Validator\PreValidatableRuleInterface; |
12
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
|
|
|
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait; |
14
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
16
|
|
|
|
17
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
18
|
|
|
final class Subset implements ParametrizedRuleInterface, PreValidatableRuleInterface |
19
|
|
|
{ |
20
|
|
|
use HandlerClassNameTrait; |
21
|
|
|
use PreValidatableTrait; |
22
|
|
|
use RuleNameTrait; |
23
|
|
|
|
24
|
1 |
|
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
|
|
|
private bool $skipOnEmpty = false, |
33
|
|
|
private bool $skipOnError = false, |
34
|
|
|
/** |
35
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
36
|
|
|
*/ |
37
|
|
|
private ?Closure $when = null, |
38
|
|
|
) { |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return iterable |
43
|
|
|
*/ |
44
|
9 |
|
public function getValues(): iterable |
45
|
|
|
{ |
46
|
9 |
|
return $this->values; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
9 |
|
public function isStrict(): bool |
53
|
|
|
{ |
54
|
9 |
|
return $this->strict; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getIterableMessage(): string |
61
|
|
|
{ |
62
|
|
|
return $this->iterableMessage; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function getSubsetMessage(): string |
69
|
|
|
{ |
70
|
3 |
|
return $this->subsetMessage; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
#[ArrayShape([ |
74
|
|
|
'values' => 'iterable', |
75
|
|
|
'strict' => 'bool', |
76
|
|
|
'iterableMessage' => 'string[]', |
77
|
|
|
'subsetMessage' => 'string[]', |
78
|
|
|
'skipOnEmpty' => 'bool', |
79
|
|
|
'skipOnError' => 'bool', |
80
|
|
|
])] |
81
|
|
|
public function getOptions(): array |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
1 |
|
'values' => $this->values, |
85
|
1 |
|
'strict' => $this->strict, |
86
|
|
|
'iterableMessage' => [ |
87
|
1 |
|
'message' => $this->iterableMessage, |
88
|
|
|
], |
89
|
|
|
'subsetMessage' => [ |
90
|
1 |
|
'message' => $this->subsetMessage, |
91
|
|
|
], |
92
|
1 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
93
|
1 |
|
'skipOnError' => $this->skipOnError, |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|