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

Composite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 17
ccs 3
cts 3
cp 1
crap 1
rs 10
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\Helper\RulesNormalizer;
11
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
13
use Yiisoft\Validator\Rule\Trait\WhenTrait;
14
use Yiisoft\Validator\RuleInterface;
15
use Yiisoft\Validator\RulesDumper;
16
use Yiisoft\Validator\RuleWithOptionsInterface;
17
use Yiisoft\Validator\SkipOnEmptyInterface;
18
use Yiisoft\Validator\SkipOnErrorInterface;
19
use Yiisoft\Validator\WhenInterface;
20
21
/**
22
 * Allows to combine and validate multiple rules.
23
 *
24
 * @psalm-import-type WhenType from WhenInterface
25
 */
26
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
class Composite implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
28
{
29
    use SkipOnEmptyTrait;
30
    use SkipOnErrorTrait;
31
    use WhenTrait;
32
33 4
    /**
34
     * @var iterable<int, RuleInterface>
35
     */
36
    private iterable $rules;
37
38
    private ?RulesDumper $rulesDumper = null;
39
40
    public function __construct(
41
        /**
42
         * @param iterable<Closure|RuleInterface>
43
         */
44
        iterable $rules = [],
45
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
46
        /**
47
         * @var bool|callable|null
48
         */
49 4
        private $skipOnEmpty = null,
50
        private bool $skipOnError = false,
51
        /**
52 1
         * @var WhenType
53
         */
54 1
        private Closure|null $when = null,
55
    ) {
56
        $this->rules = RulesNormalizer::normalizeList($rules);
57 3
    }
58
59
    public function getName(): string
60
    {
61
        return 'composite';
62
    }
63
64
    #[ArrayShape([
65 3
        'skipOnEmpty' => 'bool',
66 3
        'skipOnError' => 'bool',
67 3
        'rules' => 'array',
68
    ])]
69
    public function getOptions(): array
70
    {
71
        return [
72
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
73
            'skipOnError' => $this->skipOnError,
74 2
            'rules' => $this->getRulesDumper()->asArray($this->rules),
75
        ];
76 2
    }
77
78
    /**
79 3
     * @return iterable<int, RuleInterface>
80
     */
81 3
    public function getRules(): iterable
82
    {
83
        return $this->rules;
84
    }
85
86
    public function getHandlerClassName(): string
87
    {
88
        return CompositeHandler::class;
89
    }
90
91
    private function getRulesDumper(): RulesDumper
92
    {
93
        if ($this->rulesDumper === null) {
94
            $this->rulesDumper = new RulesDumper();
95
        }
96
97
        return $this->rulesDumper;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->rulesDumper could return the type null which is incompatible with the type-hinted return Yiisoft\Validator\RulesDumper. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
}
100