Passed
Pull Request — master (#81)
by Def
01:25
created

InRange::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\HasValidationErrorMessage;
8
use Yiisoft\Validator\Rule;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\DataSetInterface;
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 1
    public function getName(): string
87
    {
88 1
        return 'inRange';
89
    }
90
91 3
    public function getOptions(): array
92
    {
93 3
        return array_merge(
94 3
            parent::getOptions(),
95
            [
96 3
                'message' => $this->translateMessage($this->message),
97 3
                'range' => $this->range,
98 3
                'strict' => $this->strict,
99 3
                'not' => $this->not,
100
            ],
101
        );
102
    }
103
}
104