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

Subset   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
eloc 20
dl 0
loc 72
ccs 18
cts 20
cp 0.9
rs 10
c 2
b 1
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubsetMessage() 0 3 1
A isStrict() 0 3 1
A getValues() 0 3 1
A getHandlerClassName() 0 3 1
A getName() 0 3 1
A getIterableMessage() 0 3 1
A __construct() 0 19 1
A getOptions() 0 13 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
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
19
final class Subset implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
20
{
21
    use SkipOnEmptyTrait;
22
    use SkipOnErrorTrait;
23
    use WhenTrait;
24
25 2
    public function __construct(
26
        private iterable $values,
27
        /**
28
         * @var bool whether the comparison is strict (both type and value must be the same)
29
         */
30
        private bool $strict = false,
31
        private string $iterableMessage = 'Value must be iterable.',
32
        private string $subsetMessage = 'Values must be ones of {values}.',
33
34
        /**
35
         * @var bool|callable|null
36
         */
37
        private $skipOnEmpty = null,
38
        private bool $skipOnError = false,
39
        /**
40
         * @var Closure(mixed, ValidationContext):bool|null
41
         */
42
        private ?Closure $when = null,
43
    ) {
44
    }
45
46 1
    public function getName(): string
47
    {
48 1
        return 'subset';
49
    }
50
51 9
    public function getValues(): iterable
52
    {
53 9
        return $this->values;
54
    }
55
56 9
    public function isStrict(): bool
57
    {
58 9
        return $this->strict;
59
    }
60
61
    public function getIterableMessage(): string
62
    {
63
        return $this->iterableMessage;
64
    }
65
66 3
    public function getSubsetMessage(): string
67
    {
68 3
        return $this->subsetMessage;
69
    }
70
71 1
    public function getOptions(): array
72
    {
73
        return [
74 1
            'values' => $this->values,
75 1
            'strict' => $this->strict,
76
            'iterableMessage' => [
77 1
                'message' => $this->iterableMessage,
78
            ],
79
            'subsetMessage' => [
80 1
                'message' => $this->subsetMessage,
81
            ],
82 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
83 1
            'skipOnError' => $this->skipOnError,
84
        ];
85
    }
86
87 1
    public function getHandlerClassName(): string
88
    {
89 1
        return SubsetHandler::class;
90
    }
91
}
92