Passed
Push — master ( 89f940...819311 )
by Alexander
02:27
created

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