|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
8
|
|
|
use Yiisoft\Validator\DataSetInterface; |
|
9
|
|
|
use Yiisoft\Validator\HasValidationErrorMessage; |
|
10
|
|
|
use Yiisoft\Validator\Result; |
|
11
|
|
|
use Yiisoft\Validator\Rule; |
|
12
|
|
|
|
|
13
|
|
|
use function is_iterable; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* In validates that the attribute value is among a list of values. |
|
17
|
|
|
* |
|
18
|
|
|
* The range can be specified via the [[range]] property. |
|
19
|
|
|
* If the [[not]] property is set true, the validator will ensure the attribute value |
|
20
|
|
|
* is NOT among the specified range. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
class InRange extends Rule |
|
24
|
|
|
{ |
|
25
|
|
|
use HasValidationErrorMessage; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var iterable |
|
29
|
|
|
*/ |
|
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, |
|
37
|
|
|
* the attribute value should NOT be among the list of values defined via [[range]]. |
|
38
|
|
|
*/ |
|
39
|
|
|
private bool $not = false; |
|
40
|
|
|
|
|
41
|
|
|
private string $message = 'This value is invalid.'; |
|
42
|
|
|
|
|
43
|
8 |
|
public function __construct(iterable $range) |
|
44
|
|
|
{ |
|
45
|
8 |
|
$this->range = $range; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
7 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
|
49
|
|
|
{ |
|
50
|
7 |
|
$in = false; |
|
51
|
|
|
|
|
52
|
|
|
if ( |
|
53
|
7 |
|
(is_iterable($value)) && |
|
54
|
7 |
|
ArrayHelper::isSubset($value, $this->range, $this->strict) |
|
55
|
|
|
) { |
|
56
|
3 |
|
$in = true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
7 |
|
if (!$in && ArrayHelper::isIn($value, $this->range, $this->strict)) { |
|
60
|
4 |
|
$in = true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
7 |
|
$result = new Result(); |
|
64
|
|
|
|
|
65
|
7 |
|
if ($this->not === $in) { |
|
66
|
6 |
|
$result->addError($this->translateMessage($this->message)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
7 |
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
public function strict(): self |
|
73
|
|
|
{ |
|
74
|
2 |
|
$new = clone $this; |
|
75
|
2 |
|
$new->strict = true; |
|
76
|
2 |
|
return $new; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function not(): self |
|
80
|
|
|
{ |
|
81
|
1 |
|
$new = clone $this; |
|
82
|
1 |
|
$new->not = true; |
|
83
|
1 |
|
return $new; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
public function getOptions(): array |
|
87
|
|
|
{ |
|
88
|
3 |
|
return array_merge( |
|
89
|
3 |
|
parent::getOptions(), |
|
90
|
|
|
[ |
|
91
|
3 |
|
'message' => $this->translateMessage($this->message), |
|
92
|
3 |
|
'range' => $this->range, |
|
93
|
3 |
|
'strict' => $this->strict, |
|
94
|
3 |
|
'not' => $this->not, |
|
95
|
|
|
], |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|