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