Passed
Pull Request — master (#245)
by Rustam
12:07
created

Subset::isStrict()   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 JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\ParametrizedRuleInterface;
11
use Yiisoft\Validator\BeforeValidationInterface;
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
13
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
14
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
15
use Yiisoft\Validator\ValidationContext;
16
17
#[Attribute(Attribute::TARGET_PROPERTY)]
18
final class Subset implements ParametrizedRuleInterface, BeforeValidationInterface
19
{
20
    use BeforeValidationTrait;
21
    use HandlerClassNameTrait;
22
    use RuleNameTrait;
23
24 1
    public function __construct(
25
        private iterable $values,
26
        /**
27
         * @var bool whether the comparison is strict (both type and value must be the same)
28
         */
29
        private bool $strict = false,
30
        private string $iterableMessage = 'Value must be iterable.',
31
        private string $subsetMessage = 'Values must be ones of {values}.',
32
        private bool $skipOnEmpty = false,
33
        private bool $skipOnError = false,
34
        /**
35
         * @var Closure(mixed, ValidationContext):bool|null
36
         */
37
        private ?Closure $when = null,
38
    ) {
39
    }
40
41
    /**
42
     * @return iterable
43
     */
44 9
    public function getValues(): iterable
45
    {
46 9
        return $this->values;
47
    }
48
49
    /**
50
     * @return bool
51
     */
52 9
    public function isStrict(): bool
53
    {
54 9
        return $this->strict;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getIterableMessage(): string
61
    {
62
        return $this->iterableMessage;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 3
    public function getSubsetMessage(): string
69
    {
70 3
        return $this->subsetMessage;
71
    }
72
73 1
    #[ArrayShape([
74
        'values' => 'iterable',
75
        'strict' => 'bool',
76
        'iterableMessage' => 'string[]',
77
        'subsetMessage' => 'string[]',
78
        'skipOnEmpty' => 'bool',
79
        'skipOnError' => 'bool',
80
    ])]
81
    public function getOptions(): array
82
    {
83
        return [
84 1
            'values' => $this->values,
85 1
            'strict' => $this->strict,
86
            'iterableMessage' => [
87 1
                'message' => $this->iterableMessage,
88
            ],
89
            'subsetMessage' => [
90 1
                'message' => $this->subsetMessage,
91
            ],
92 1
            'skipOnEmpty' => $this->skipOnEmpty,
93 1
            'skipOnError' => $this->skipOnError,
94
        ];
95
    }
96
}
97