Passed
Pull Request — master (#222)
by Dmitriy
02:26
created

Each::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
dl 0
loc 11
ccs 1
cts 1
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 6
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Each;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\RuleNameTrait;
10
use Yiisoft\Validator\Rule\HandlerClassNameTrait;
11
use Yiisoft\Validator\RuleInterface;
12
13
/**
14
 * Validates an array by checking each of its elements against a set of rules.
15
 */
16
#[Attribute(Attribute::TARGET_PROPERTY)]
17
final class Each implements RuleInterface
18
{
19
    use HandlerClassNameTrait;
20
    use RuleNameTrait;
21
22 1
    public function __construct(
23
        /**
24
         * @var iterable<RuleInterface|RuleInterface[]>
25
         */
26
        public iterable $rules = [],
27
        public string $incorrectInputMessage = 'Value should be array or iterable.',
28
        public string $message = '{error} {value} given.',
29
        public bool $skipOnEmpty = false,
30
        public bool $skipOnError = false,
31
        public ?Closure $when = null,
32
    ) {
33
    }
34
35 1
    public function getOptions(): array
36
    {
37 1
        $arrayOfRules = [];
38 1
        foreach ($this->rules as $rule) {
39 1
            if ($rule instanceof RuleInterface) {
40 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
41
            } else {
42
                $arrayOfRules[] = [get_class($rule)];
43
            }
44
        }
45 1
        return $arrayOfRules;
46
    }
47
}
48