Test Failed
Pull Request — master (#219)
by
unknown
02:42
created

InRange::strict()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
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 Attribute;
8
use Yiisoft\Validator\FormatterInterface;
9
use Yiisoft\Validator\ValidationContext;
10
use Yiisoft\Arrays\ArrayHelper;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\Rule;
13
14
/**
15
 * Validates that the value is among a list of values.
16
 *
17
 * The range can be specified via constructor.
18
 * If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class InRange extends Rule
22
{
23 30
    public function __construct(
24
        private iterable $range,
25
        /**
26
         * @var bool whether the comparison is strict (both type and value must be the same)
27
         */
28
        private bool $strict = false,
29
        /**
30
         * @var bool whether to invert the validation logic. Defaults to false. If set to `true`, the value should NOT
31
         * be among the list of values passed via constructor.
32
         */
33
        private bool $not = false,
34
        private string $message = 'This value is invalid.',
35
        ?FormatterInterface $formatter = null,
36
        bool $skipOnEmpty = false,
37
        bool $skipOnError = false,
38
        $when = null
39
    ) {
40 30
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
41
    }
42
43 30
    /**
44
     * @see $range
45 30
     */
46
    public function range(iterable $value): self
47 30
    {
48 18
        $new = clone $this;
49
        $new->range = $value;
50
51 30
        return $new;
52
    }
53
54 3
    /**
55
     * @see $strict
56 3
     */
57 3
    public function strict(bool $value): self
58 3
    {
59 3
        $new = clone $this;
60 3
        $new->strict = $value;
61
62
        return $new;
63
    }
64
65
    /**
66
     * @see $not
67
     */
68
    public function not(bool $value): self
69
    {
70
        $new = clone $this;
71
        $new->not = $value;
72
73
        return $new;
74
    }
75
76
    /**
77
     * @see $message
78
     */
79
    public function message(string $value): self
80
    {
81
        $new = clone $this;
82
        $new->message = $value;
83
84
        return $new;
85
    }
86
87
    protected function validateValue($value, ?ValidationContext $context = null): Result
88
    {
89
        $result = new Result();
90
91
        if ($this->not === ArrayHelper::isIn($value, $this->range, $this->strict)) {
92
            $result->addError($this->formatMessage($this->message));
93
        }
94
95
        return $result;
96
    }
97
98
    public function getOptions(): array
99
    {
100
        return array_merge(parent::getOptions(), [
101
            'range' => $this->range,
102
            'strict' => $this->strict,
103
            'not' => $this->not,
104
            'message' => $this->formatMessage($this->message),
105
        ]);
106
    }
107
}
108