Passed
Pull Request — master (#81)
by Def
04:12 queued 02:54
created

InRange::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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
    /**
87
     * @inheritDoc
88
     * @return string
89
     */
90 1
    public function getName(): string
91
    {
92 1
        return 'inRange';
93
    }
94
95
    /**
96
     * @inheritDoc
97
     * @return array
98
     */
99 3
    public function getOptions(): array
100
    {
101 3
        return array_merge(
102 3
            ['range' => $this->range],
103 3
            ['message' => $this->translateMessage($this->message)],
104 3
            $this->strict ? ['strict' => true] : [],
105 3
            $this->not ? ['not' => true] : [],
106 3
            parent::getOptions()
107
        );
108
    }
109
}
110