Passed
Push — master ( 8c7a41...68be67 )
by Sergei
02:44
created

Composite   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 86
ccs 12
cts 12
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A afterInitAttribute() 0 5 3
A getRulesDumper() 0 7 2
A getOptions() 0 11 1
A getRules() 0 3 1
A getName() 0 3 1
A getHandlerClassName() 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 JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\AfterInitAttributeEventInterface;
11
use Yiisoft\Validator\Helper\RulesNormalizer;
12
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
13
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
14
use Yiisoft\Validator\Rule\Trait\WhenTrait;
15
use Yiisoft\Validator\RuleInterface;
16
use Yiisoft\Validator\RulesDumper;
17
use Yiisoft\Validator\RuleWithOptionsInterface;
18
use Yiisoft\Validator\SkipOnEmptyInterface;
19
use Yiisoft\Validator\SkipOnErrorInterface;
20
use Yiisoft\Validator\WhenInterface;
21
22
/**
23
 * Allows to combine and validate multiple rules.
24
 *
25
 * @psalm-import-type WhenType from WhenInterface
26
 */
27
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
28
class Composite implements
29
    RuleWithOptionsInterface,
30
    SkipOnErrorInterface,
31
    WhenInterface,
32
    SkipOnEmptyInterface,
33 4
    AfterInitAttributeEventInterface
34
{
35
    use SkipOnEmptyTrait;
36
    use SkipOnErrorTrait;
37
    use WhenTrait;
38
39
    /**
40
     * @var iterable<int, RuleInterface>
41
     */
42
    private iterable $rules;
43
44
    private ?RulesDumper $rulesDumper = null;
45
46
    public function __construct(
47
        /**
48
         * @param iterable<Closure|RuleInterface>
49 4
         */
50
        iterable $rules = [],
51
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
52 1
        /**
53
         * @var bool|callable|null
54 1
         */
55
        private $skipOnEmpty = null,
56
        private bool $skipOnError = false,
57 3
        /**
58
         * @var WhenType
59
         */
60
        private Closure|null $when = null,
61
    ) {
62
        $this->rules = RulesNormalizer::normalizeList($rules);
63
    }
64
65 3
    public function getName(): string
66 3
    {
67 3
        return 'composite';
68
    }
69
70
    #[ArrayShape([
71
        'skipOnEmpty' => 'bool',
72
        'skipOnError' => 'bool',
73
        'rules' => 'array',
74 2
    ])]
75
    public function getOptions(): array
76 2
    {
77
        return [
78
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
79 3
            'skipOnError' => $this->skipOnError,
80
            'rules' => $this->getRulesDumper()->asArray($this->rules),
81 3
        ];
82
    }
83
84
    /**
85
     * @return iterable<int, RuleInterface>
86
     */
87
    public function getRules(): iterable
88
    {
89
        return $this->rules;
90
    }
91
92
    public function getHandlerClassName(): string
93
    {
94
        return CompositeHandler::class;
95
    }
96
97
    public function afterInitAttribute(object $object): void
98
    {
99
        foreach ($this->rules as $rule) {
100
            if ($rule instanceof AfterInitAttributeEventInterface) {
101
                $rule->afterInitAttribute($object);
102
            }
103
        }
104
    }
105
106
    private function getRulesDumper(): RulesDumper
107
    {
108
        if ($this->rulesDumper === null) {
109
            $this->rulesDumper = new RulesDumper();
110
        }
111
112
        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...
113
    }
114
}
115