Test Failed
Pull Request — master (#364)
by
unknown
03:02
created

Each::getIncorrectInputMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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