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