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

Subset::getHandlerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
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 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