Passed
Push — master ( f0b656...d1524d )
by Alexander
02:42
created

Each::propagateOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 0
crap 3
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\BeforeValidationInterface;
11
use Yiisoft\Validator\PropagateOptionsInterface;
12
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
15
use Yiisoft\Validator\RuleInterface;
16
use Yiisoft\Validator\SerializableRuleInterface;
17
use Yiisoft\Validator\SkipOnEmptyInterface;
18
use Yiisoft\Validator\ValidationContext;
19
20
/**
21
 * Validates an array by checking each of its elements against a set of rules.
22
 */
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class Each implements
25
    SerializableRuleInterface,
26
    BeforeValidationInterface,
27
    SkipOnEmptyInterface,
28
    PropagateOptionsInterface
29
{
30
    use BeforeValidationTrait;
31
    use RuleNameTrait;
32
    use SkipOnEmptyTrait;
33
34 5
    public function __construct(
35
        /**
36
         * @var iterable<BeforeValidationInterface|RuleInterface|SkipOnEmptyInterface>
37
         */
38
        private iterable $rules = [],
39
        private string $incorrectInputMessage = 'Value must be array or iterable.',
40
        private string $message = '{error} {value} given.',
41
42
        /**
43
         * @var bool|callable|null
44
         */
45
        private $skipOnEmpty = null,
46
        private bool $skipOnError = false,
47
        /**
48
         * @var Closure(mixed, ValidationContext):bool|null
49
         */
50
        private ?Closure $when = null,
51
    ) {
52
    }
53
54 1
    public function propagateOptions(): void
55
    {
56 1
        $rules = [];
57 1
        foreach ($this->rules as $rule) {
58 1
            $rule = $rule->skipOnEmpty($this->skipOnEmpty);
59 1
            $rule = $rule->skipOnError($this->skipOnError);
60 1
            $rule = $rule->when($this->when);
61
62 1
            $rules[] = $rule;
63
64 1
            if ($rule instanceof PropagateOptionsInterface) {
65 1
                $rule->propagateOptions();
66
            }
67
        }
68
69 1
        $this->rules = $rules;
70
    }
71
72
    /**
73
     * @return iterable<BeforeValidationInterface|BeforeValidationInterface[]|\Closure|\Closure[]|RuleInterface|RuleInterface[]|SkipOnEmptyInterface|SkipOnEmptyInterface[]>
74
     */
75 10
    public function getRules(): iterable
76
    {
77 10
        return $this->rules;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 3
    public function getIncorrectInputMessage(): string
84
    {
85 3
        return $this->incorrectInputMessage;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 9
    public function getMessage(): string
92
    {
93 9
        return $this->message;
94
    }
95
96 2
    #[ArrayShape([
97
        'incorrectInputMessage' => 'array',
98
        'message' => 'array',
99
        'skipOnEmpty' => 'bool',
100
        'skipOnError' => 'bool',
101
        'rules' => 'array',
102
    ])]
103
    public function getOptions(): array
104
    {
105 2
        $arrayOfRules = [];
106 2
        foreach ($this->rules as $rule) {
107 2
            if ($rule instanceof SerializableRuleInterface) {
108 2
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
109
            } else {
110
                $arrayOfRules[] = [$rule->getName()];
111
            }
112
        }
113
114
        return [
115
            'incorrectInputMessage' => [
116 2
                'message' => $this->getIncorrectInputMessage(),
117
            ],
118
            'message' => [
119 2
                'message' => $this->getMessage(),
120
            ],
121 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
122 2
            'skipOnError' => $this->skipOnError,
123
            'rules' => $arrayOfRules,
124
        ];
125
    }
126
127 7
    public function getHandlerClassName(): string
128
    {
129 7
        return EachHandler::class;
130
    }
131
}
132