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