|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule\InRange; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Yiisoft\Validator\Rule\RuleNameTrait; |
|
10
|
|
|
use Yiisoft\Validator\Rule\HandlerClassNameTrait; |
|
11
|
|
|
use Yiisoft\Validator\RuleInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Validates that the value is among a list of values. |
|
15
|
|
|
* |
|
16
|
|
|
* The range can be specified via constructor. |
|
17
|
|
|
* If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range. |
|
18
|
|
|
*/ |
|
19
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
|
20
|
|
|
final class InRange implements RuleInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use HandlerClassNameTrait; |
|
23
|
|
|
use RuleNameTrait; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
public iterable $range, |
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool whether the comparison is strict (both type and value must be the same) |
|
29
|
|
|
*/ |
|
30
|
|
|
public bool $strict = false, |
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool whether to invert the validation logic. Defaults to false. If set to `true`, the value should NOT |
|
33
|
|
|
* be among the list of values passed via constructor. |
|
34
|
|
|
*/ |
|
35
|
|
|
public bool $not = false, |
|
36
|
|
|
public string $message = 'This value is invalid.', |
|
37
|
|
|
public bool $skipOnEmpty = false, |
|
38
|
|
|
public bool $skipOnError = false, |
|
39
|
|
|
public ?Closure $when = null, |
|
40
|
|
|
) { |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
public function getOptions(): array |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
3 |
|
'range' => $this->range, |
|
47
|
3 |
|
'strict' => $this->strict, |
|
48
|
3 |
|
'not' => $this->not, |
|
49
|
|
|
'message' => [ |
|
50
|
3 |
|
'message' => $this->message, |
|
51
|
|
|
], |
|
52
|
3 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
|
53
|
3 |
|
'skipOnError' => $this->skipOnError, |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|