Passed
Push — master ( 9562b0...4734f7 )
by Sergei
04:27 queued 02:02
created

Subset   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getValues() 0 3 1
A getName() 0 3 1
A getIterableMessage() 0 3 1
A getSubsetMessage() 0 3 1
A __construct() 0 22 1
A isStrict() 0 3 1
A getOptions() 0 15 1
A getHandler() 0 3 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\RuleWithOptionsInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\WhenInterface;
16
17
/**
18
 * @psalm-import-type WhenType from WhenInterface
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
21
final class Subset implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
22
{
23
    use SkipOnEmptyTrait;
24
    use SkipOnErrorTrait;
25 3
    use WhenTrait;
26
27
    public function __construct(
28
        /**
29
         * @var iterable<scalar>
30
         */
31
        private iterable $values,
32
        /**
33
         * @var bool whether the comparison is strict (both type and value must be the same)
34
         */
35
        private bool $strict = false,
36
        private string $iterableMessage = 'Value must be iterable.',
37
        private string $subsetMessage = 'Values must be ones of {values}.',
38
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
39
        /**
40
         * @var bool|callable|null
41
         */
42
        private $skipOnEmpty = null,
43
        private bool $skipOnError = false,
44
        /**
45
         * @var WhenType
46
         */
47
        private Closure|null $when = null,
48
    ) {
49 1
    }
50
51 1
    public function getName(): string
52
    {
53
        return 'subset';
54
    }
55
56
    /**
57 12
     * @return iterable<scalar>
58
     */
59 12
    public function getValues(): iterable
60
    {
61
        return $this->values;
62 12
    }
63
64 12
    public function isStrict(): bool
65
    {
66
        return $this->strict;
67 4
    }
68
69 4
    public function getIterableMessage(): string
70
    {
71
        return $this->iterableMessage;
72 6
    }
73
74 6
    public function getSubsetMessage(): string
75
    {
76
        return $this->subsetMessage;
77 1
    }
78
79
    public function getOptions(): array
80 1
    {
81 1
        return [
82
            'values' => $this->values,
83 1
            'strict' => $this->strict,
84
            'iterableMessage' => [
85
                'template' => $this->iterableMessage,
86
                'parameters' => [],
87 1
            ],
88
            'subsetMessage' => [
89
                'template' => $this->subsetMessage,
90 1
                'parameters' => [],
91 1
            ],
92
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
93
            'skipOnError' => $this->skipOnError,
94
        ];
95 16
    }
96
97 16
    public function getHandler(): string
98
    {
99
        return SubsetHandler::class;
100
    }
101
}
102