Passed
Pull Request — master (#446)
by Sergei
08:07 queued 05:40
created

Each::afterInitAttribute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
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\PropagateOptionsInterface;
13
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
14
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
15
use Yiisoft\Validator\Rule\Trait\WhenTrait;
16
use Yiisoft\Validator\RuleInterface;
17
use Yiisoft\Validator\RulesDumper;
18
use Yiisoft\Validator\RuleWithOptionsInterface;
19
use Yiisoft\Validator\SkipOnEmptyInterface;
20
use Yiisoft\Validator\SkipOnErrorInterface;
21
use Yiisoft\Validator\WhenInterface;
22
23
/**
24
 * Validates an array by checking each of its elements against a set of rules.
25
 *
26
 * @psalm-import-type WhenType from WhenInterface
27
 */
28
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
29
final class Each implements
30
    RuleWithOptionsInterface,
31
    SkipOnErrorInterface,
32
    WhenInterface,
33
    SkipOnEmptyInterface,
34
    PropagateOptionsInterface,
35
    AfterInitAttributeEventInterface
36
{
37
    use SkipOnEmptyTrait;
38
    use SkipOnErrorTrait;
39 6
    use WhenTrait;
40
41
    /**
42
     * @var iterable<RuleInterface>
43
     */
44
    private iterable $rules;
45
46
    private ?RulesDumper $rulesDumper = null;
47
48
    public function __construct(
49
        /**
50
         * @param callable|iterable<callable|RuleInterface>|RuleInterface $rules
51
         */
52
        iterable|callable|RuleInterface $rules = [],
53
        private string $incorrectInputMessage = 'Value must be array or iterable.',
54
        private string $incorrectInputKeyMessage = 'Every iterable key must have an integer or a string type.',
55
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
56
        /**
57 6
         * @var bool|callable|null
58
         */
59
        private $skipOnEmpty = null,
60 2
        private bool $skipOnError = false,
61
        /**
62 2
         * @var WhenType
63
         */
64
        private Closure|null $when = null,
65 1
    ) {
66
        $this->rules = RulesNormalizer::normalizeList($rules);
67 1
    }
68 1
69 1
    public function getName(): string
70 1
    {
71
        return 'each';
72 1
    }
73 1
74
    public function propagateOptions(): void
75 1
    {
76 1
        $rules = [];
77
        foreach ($this->rules as $rule) {
78
            if ($rule instanceof SkipOnEmptyInterface) {
79 1
                $rule = $rule->skipOnEmpty($this->skipOnEmpty);
80
            }
81 1
            if ($rule instanceof SkipOnErrorInterface) {
82 1
                $rule = $rule->skipOnError($this->skipOnError);
83
            }
84
            if ($rule instanceof WhenInterface) {
85
                $rule = $rule->when($this->when);
86 1
            }
87
88
            $rules[] = $rule;
89
90
            if ($rule instanceof PropagateOptionsInterface) {
91
                $rule->propagateOptions();
92 18
            }
93
        }
94 18
95
        $this->rules = $rules;
96
    }
97 5
98
    /**
99 5
     * @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>
100
     */
101
    public function getRules(): iterable
102 3
    {
103
        return $this->rules;
104 3
    }
105
106
    public function getIncorrectInputMessage(): string
107 3
    {
108
        return $this->incorrectInputMessage;
109
    }
110
111
    public function getIncorrectInputKeyMessage(): string
112
    {
113
        return $this->incorrectInputKeyMessage;
114
    }
115
116
    #[ArrayShape([
117
        'incorrectInputMessage' => 'array',
118 3
        'incorrectInputKeyMessage' => 'array',
119
        'skipOnEmpty' => 'bool',
120
        'skipOnError' => 'bool',
121
        'rules' => 'array',
122 3
    ])]
123
    public function getOptions(): array
124
    {
125 3
        return [
126 3
            'incorrectInputMessage' => [
127 3
                'template' => $this->incorrectInputMessage,
128
                'parameters' => [],
129
            ],
130
            'incorrectInputKeyMessage' => [
131 18
                'template' => $this->incorrectInputKeyMessage,
132
                'parameters' => [],
133 18
            ],
134
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
135
            'skipOnError' => $this->skipOnError,
136
            'rules' => $this->getRulesDumper()->asArray($this->rules),
137
        ];
138
    }
139
140
    public function getHandlerClassName(): string
141
    {
142
        return EachHandler::class;
143
    }
144
145
    public function afterInitAttribute(object $object, int $target): void
146
    {
147
        foreach ($this->rules as $rule) {
148
            if ($rule instanceof AfterInitAttributeEventInterface) {
149
                $rule->afterInitAttribute(
150
                    $object,
151
                    $target === Attribute::TARGET_CLASS ? Attribute::TARGET_PROPERTY : $target
152
                );
153
            }
154
        }
155
    }
156
157
    private function getRulesDumper(): RulesDumper
158
    {
159
        if ($this->rulesDumper === null) {
160
            $this->rulesDumper = new RulesDumper();
161
        }
162
163
        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...
164
    }
165
}
166