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