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

InRange   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 19
dl 0
loc 74
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 3 1
A isStrict() 0 3 1
A getOptions() 0 11 1
A getHandlerClassName() 0 3 1
A __construct() 0 23 1
A getRange() 0 3 1
A isNot() 0 3 1
A getName() 0 3 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