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

Each   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

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