Total Complexity | 11 |
Total Lines | 78 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
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 |
|
84 | } |
||
85 | |||
86 | 1 | public function getName(): string |
|
87 | { |
||
89 | } |
||
90 | |||
91 | 3 | public function getOptions(): array |
|
104 |