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