Passed
Pull Request — master (#333)
by Sergei
02:38
created

InRange::isNot()   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
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
/**
19
 * Validates that the value is among a list of values.
20
 *
21
 * The range can be specified via constructor.
22
 * If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class InRange implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
26
{
27
    use SkipOnEmptyTrait;
28
    use SkipOnErrorTrait;
29
    use WhenTrait;
30
31 2
    public function __construct(
32
        private iterable $range,
33
        /**
34
         * @var bool whether the comparison is strict (both type and value must be the same)
35
         */
36
        private bool $strict = false,
37
        /**
38
         * @var bool whether to invert the validation logic. Defaults to false. If set to `true`, the value should NOT
39
         * be among the list of values passed via constructor.
40
         */
41
        private bool $not = false,
42
        private string $message = 'This value is invalid.',
43
44
        /**
45
         * @var bool|callable|null
46
         */
47
        private $skipOnEmpty = null,
48
        private bool $skipOnError = false,
49
        /**
50
         * @var Closure(mixed, ValidationContext):bool|null
51
         */
52
        private ?Closure $when = null,
53
    ) {
54
    }
55
56 1
    public function getName(): string
57
    {
58 1
        return 'inRange';
59
    }
60
61 38
    public function getRange(): iterable
62
    {
63 38
        return $this->range;
64
    }
65
66 38
    public function isStrict(): bool
67
    {
68 38
        return $this->strict;
69
    }
70
71 38
    public function isNot(): bool
72
    {
73 38
        return $this->not;
74
    }
75
76 22
    public function getMessage(): string
77
    {
78 22
        return $this->message;
79
    }
80
81 3
    public function getOptions(): array
82
    {
83
        return [
84 3
            'range' => $this->range,
85 3
            'strict' => $this->strict,
86 3
            'not' => $this->not,
87
            'message' => [
88 3
                'message' => $this->message,
89
            ],
90 3
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
91 3
            'skipOnError' => $this->skipOnError,
92
        ];
93
    }
94
95 10
    public function getHandlerClassName(): string
96
    {
97 10
        return InRangeHandler::class;
98
    }
99
}
100