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

Subset   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 15
dl 0
loc 45
ccs 10
cts 18
cp 0.5556
rs 10
c 2
b 1
f 0
wmc 6
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\PreValidatableRuleInterface;
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 12 at column 27
Loading history...
13
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
14
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
15
16
#[Attribute(Attribute::TARGET_PROPERTY)]
17
final class Subset implements ParametrizedRuleInterface, PreValidatableRuleInterface
18
{
19
    use HandlerClassNameTrait;
20
    use PreValidatableTrait;
21
    use RuleNameTrait;
22
23 1
    public function __construct(
24
        private iterable $values,
25
        /**
26
         * @var bool whether the comparison is strict (both type and value must be the same)
27
         */
28
        private bool $strict = false,
29
        private string $iterableMessage = 'Value must be iterable.',
30
        private string $subsetMessage = 'Values must be ones of {values}.',
31
        private bool $skipOnEmpty = false,
32
        private bool $skipOnError = false,
33
        private ?Closure $when = null,
34
    ) {
35
    }
36
37
    /**
38
     * @return iterable
39
     */
40 9
    public function getValues(): iterable
41
    {
42 9
        return $this->values;
43
    }
44
45
    /**
46
     * @return bool
47
     */
48 9
    public function isStrict(): bool
49
    {
50 9
        return $this->strict;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getIterableMessage(): string
57
    {
58
        return $this->iterableMessage;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 3
    public function getSubsetMessage(): string
65
    {
66 3
        return $this->subsetMessage;
67
    }
68
69 1
    #[ArrayShape([
70
        'values' => 'iterable',
71
        'strict' => 'bool',
72
        'iterableMessage' => 'string[]',
73
        'subsetMessage' => 'string[]',
74
        'skipOnEmpty' => 'bool',
75
        'skipOnError' => 'bool',
76
    ])]
77
    public function getOptions(): array
78
    {
79
        return [
80 1
            'values' => $this->values,
81 1
            'strict' => $this->strict,
82
            'iterableMessage' => [
83 1
                'message' => $this->iterableMessage,
84
            ],
85
            'subsetMessage' => [
86 1
                'message' => $this->subsetMessage,
87
            ],
88 1
            'skipOnEmpty' => $this->skipOnEmpty,
89 1
            'skipOnError' => $this->skipOnError,
90
        ];
91
    }
92
}
93