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