Passed
Pull Request — master (#99)
by Def
02:08
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\ErrorMessage;
8
use function is_iterable;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Validator\DataSetInterface;
11
use Yiisoft\Validator\HasValidationErrorMessage;
12
use Yiisoft\Validator\Result;
13
14
use Yiisoft\Validator\Rule;
15
16
/**
17
 * In validates that the attribute value is among a list of values.
18
 *
19
 * The range can be specified via the [[range]] property.
20
 * If the [[not]] property is set true, the validator will ensure the attribute value
21
 * is NOT among the specified range.
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 8
    }
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(new ErrorMessage($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 getRawOptions(): array
87
    {
88 3
        return array_merge(
89 3
            parent::getRawOptions(),
90
            [
91 3
                'message' => new ErrorMessage($this->message),
92 3
                'range' => $this->range,
93 3
                'strict' => $this->strict,
94 3
                'not' => $this->not,
95
            ],
96
        );
97
    }
98
}
99