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

Each::propagateOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

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