Passed
Pull Request — master (#99)
by Def
02:39
created

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