Passed
Pull Request — master (#468)
by Sergei
03:08
created

Composite::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
ccs 1
cts 1
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\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\Helper\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
    protected iterable $rules = [];
43
44
    /**
45
     * @var bool|callable|null
46
     */
47
    protected $skipOnEmpty = null;
48
49 4
    protected bool $skipOnError = false;
50
51
    /**
52 1
     * @psalm-var WhenType
53
     */
54 1
    protected Closure|null $when = null;
55
56
    private ?RulesDumper $rulesDumper = null;
57 3
58
    /**
59
     * @param iterable<Closure|RuleInterface> $rules
60
     *
61
     * @psalm-param WhenType $when
62
     */
63
    public function __construct(
64
        iterable $rules = [],
65 3
        bool|callable|null $skipOnEmpty = null,
66 3
        bool $skipOnError = false,
67 3
        Closure|null $when = null,
68
    ) {
69
        $this->rules = RulesNormalizer::normalizeList($rules);
70
        $this->skipOnEmpty = $skipOnEmpty;
71
        $this->skipOnError = $skipOnError;
72
        $this->when = $when;
73
    }
74 2
75
    public function getName(): string
76 2
    {
77
        return 'composite';
78
    }
79 3
80
    #[ArrayShape([
81 3
        'skipOnEmpty' => 'bool',
82
        'skipOnError' => 'bool',
83
        'rules' => 'array',
84
    ])]
85
    public function getOptions(): array
86
    {
87
        return [
88
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
89
            'skipOnError' => $this->skipOnError,
90
            'rules' => $this->dumpRulesAsArray(),
91
        ];
92
    }
93
94
    /**
95
     * @return iterable<int, RuleInterface>
96
     */
97
    public function getRules(): iterable
98
    {
99
        return $this->rules;
100
    }
101
102
    final public function getHandler(): string
103
    {
104
        return CompositeHandler::class;
105
    }
106
107
    public function afterInitAttribute(object $object, int $target): void
108
    {
109
        foreach ($this->getRules() as $rule) {
110
            if ($rule instanceof AfterInitAttributeEventInterface) {
111
                $rule->afterInitAttribute($object, $target);
112
            }
113
        }
114
    }
115
116
    final protected function dumpRulesAsArray(): array
117
    {
118
        return $this->getRulesDumper()->asArray($this->getRules());
119
    }
120
121
    private function getRulesDumper(): RulesDumper
122
    {
123
        if ($this->rulesDumper === null) {
124
            $this->rulesDumper = new RulesDumper();
125
        }
126
127
        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\Helper\RulesDumper. Consider adding an additional type-check to rule them out.
Loading history...
128
    }
129
}
130