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\Rule\Trait\RuleNameTrait; |
|
|
|
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
12
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validates that the value is among a list of values. |
16
|
|
|
* |
17
|
|
|
* The range can be specified via constructor. |
18
|
|
|
* If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range. |
19
|
|
|
*/ |
20
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
21
|
|
|
final class InRange implements ParametrizedRuleInterface |
22
|
|
|
{ |
23
|
|
|
use HandlerClassNameTrait; |
24
|
|
|
use RuleNameTrait; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
private iterable $range, |
28
|
|
|
/** |
29
|
|
|
* @var bool whether the comparison is strict (both type and value must be the same) |
30
|
|
|
*/ |
31
|
|
|
private bool $strict = false, |
32
|
|
|
/** |
33
|
|
|
* @var bool whether to invert the validation logic. Defaults to false. If set to `true`, the value should NOT |
34
|
|
|
* be among the list of values passed via constructor. |
35
|
|
|
*/ |
36
|
|
|
private bool $not = false, |
37
|
|
|
private string $message = 'This value is invalid.', |
38
|
|
|
private bool $skipOnEmpty = false, |
39
|
|
|
private bool $skipOnError = false, |
40
|
|
|
private ?Closure $when = null, |
41
|
|
|
) { |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return iterable |
46
|
|
|
*/ |
47
|
30 |
|
public function getRange(): iterable |
48
|
|
|
{ |
49
|
30 |
|
return $this->range; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
30 |
|
public function isStrict(): bool |
56
|
|
|
{ |
57
|
30 |
|
return $this->strict; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
30 |
|
public function isNot(): bool |
64
|
|
|
{ |
65
|
30 |
|
return $this->not; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
18 |
|
public function getMessage(): string |
72
|
|
|
{ |
73
|
18 |
|
return $this->message; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
3 |
|
public function isSkipOnEmpty(): bool |
80
|
|
|
{ |
81
|
3 |
|
return $this->skipOnEmpty; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function isSkipOnError(): bool |
88
|
|
|
{ |
89
|
|
|
return $this->skipOnError; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Closure|null |
94
|
|
|
*/ |
95
|
|
|
public function getWhen(): ?Closure |
96
|
|
|
{ |
97
|
|
|
return $this->when; |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
#[ArrayShape([ |
101
|
|
|
'range' => 'iterable', |
102
|
|
|
'strict' => 'bool', |
103
|
|
|
'not' => 'bool', |
104
|
|
|
'message' => 'string[]', |
105
|
|
|
'skipOnEmpty' => 'bool', |
106
|
|
|
'skipOnError' => 'bool', |
107
|
|
|
])] |
108
|
|
|
public function getOptions(): array |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
3 |
|
'range' => $this->range, |
112
|
3 |
|
'strict' => $this->strict, |
113
|
3 |
|
'not' => $this->not, |
114
|
|
|
'message' => [ |
115
|
3 |
|
'message' => $this->message, |
116
|
|
|
], |
117
|
3 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
118
|
3 |
|
'skipOnError' => $this->skipOnError, |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|