|
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
|
|
|
/** |
|
19
|
|
|
* Validates that the value is one of the values provided in {@see $values}. |
|
20
|
|
|
* If the {@see In::$not} is set, the validation logic is inverted and the rule will ensure that the value is NOT one of |
|
21
|
|
|
* them. |
|
22
|
|
|
*/ |
|
23
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
24
|
|
|
final class In implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use SkipOnEmptyTrait; |
|
27
|
|
|
use SkipOnErrorTrait; |
|
28
|
|
|
use WhenTrait; |
|
29
|
|
|
|
|
30
|
3 |
|
public function __construct( |
|
31
|
|
|
/** |
|
32
|
|
|
* @var iterable<scalar> |
|
33
|
|
|
*/ |
|
34
|
|
|
private iterable $values, |
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool Whether the comparison is strict (both type and value must be the same) |
|
37
|
|
|
*/ |
|
38
|
|
|
private bool $strict = false, |
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool Whether to invert the validation logic. Defaults to `false`. If set to `true`, the value must NOT |
|
41
|
|
|
* be among the list of {@see $values}. |
|
42
|
|
|
*/ |
|
43
|
|
|
private bool $not = false, |
|
44
|
|
|
private string $message = 'This value is invalid.', |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var bool|callable|null |
|
48
|
|
|
*/ |
|
49
|
|
|
private $skipOnEmpty = null, |
|
50
|
|
|
private bool $skipOnError = false, |
|
51
|
|
|
/** |
|
52
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
53
|
|
|
*/ |
|
54
|
|
|
private ?Closure $when = null, |
|
55
|
|
|
) { |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function getName(): string |
|
59
|
|
|
{ |
|
60
|
1 |
|
return 'inRange'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
38 |
|
public function getValues(): iterable |
|
64
|
|
|
{ |
|
65
|
38 |
|
return $this->values; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
38 |
|
public function isStrict(): bool |
|
69
|
|
|
{ |
|
70
|
38 |
|
return $this->strict; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
38 |
|
public function isNot(): bool |
|
74
|
|
|
{ |
|
75
|
38 |
|
return $this->not; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
22 |
|
public function getMessage(): string |
|
79
|
|
|
{ |
|
80
|
22 |
|
return $this->message; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
public function getOptions(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
3 |
|
'values' => $this->values, |
|
87
|
3 |
|
'strict' => $this->strict, |
|
88
|
3 |
|
'not' => $this->not, |
|
89
|
|
|
'message' => [ |
|
90
|
3 |
|
'message' => $this->message, |
|
91
|
|
|
], |
|
92
|
3 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
93
|
3 |
|
'skipOnError' => $this->skipOnError, |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
38 |
|
public function getHandlerClassName(): string |
|
98
|
|
|
{ |
|
99
|
38 |
|
return InHandler::class; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|