Passed
Pull Request — master (#222)
by Dmitriy
02:23
created

InRange   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 4
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\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 9 at column 27
Loading history...
10
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
11
use Yiisoft\Validator\RuleInterface;
12
13
/**
14
 * Validates that the value is among a list of values.
15
 *
16
 * The range can be specified via constructor.
17
 * If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range.
18
 */
19
#[Attribute(Attribute::TARGET_PROPERTY)]
20
final class InRange implements RuleInterface
21
{
22
    use HandlerClassNameTrait;
23
    use RuleNameTrait;
24
25
    public function __construct(
26
        public iterable $range,
27
        /**
28
         * @var bool whether the comparison is strict (both type and value must be the same)
29
         */
30
        public bool $strict = false,
31
        /**
32
         * @var bool whether to invert the validation logic. Defaults to false. If set to `true`, the value should NOT
33
         * be among the list of values passed via constructor.
34
         */
35
        public bool $not = false,
36
        public string $message = 'This value is invalid.',
37
        public bool $skipOnEmpty = false,
38
        public bool $skipOnError = false,
39
        public ?Closure $when = null,
40
    ) {
41
    }
42
43 3
    public function getOptions(): array
44
    {
45
        return [
46 3
            'range' => $this->range,
47 3
            'strict' => $this->strict,
48 3
            'not' => $this->not,
49
            'message' => [
50 3
                'message' => $this->message,
51
            ],
52 3
            'skipOnEmpty' => $this->skipOnEmpty,
53 3
            'skipOnError' => $this->skipOnError,
54
        ];
55
    }
56
}
57