Passed
Pull Request — master (#222)
by Alexander
04:51 queued 02:32
created

InRange::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 16
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\InRange;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\RuleNameTrait;
10
use Yiisoft\Validator\Rule\ValidatorClassNameTrait;
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 RuleNameTrait;
23
    use ValidatorClassNameTrait;
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