Passed
Pull Request — master (#317)
by
unknown
02:22
created

Each::propagateOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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