Passed
Pull Request — master (#222)
by Alexander
04:51 queued 02:32
created

Each::getOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
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\ValidatorClassNameTrait;
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 RuleNameTrait;
20
    use ValidatorClassNameTrait;
21
22 1
    public function __construct(
23
        public iterable $rules = [],
24
        public string $incorrectInputMessage = 'Value should be array or iterable.',
25
        public string $message = '{error} {value} given.',
26
        public bool $skipOnEmpty = false,
27
        public bool $skipOnError = false,
28
        public ?Closure $when = null,
29
    ) {
30
    }
31
32 1
    public function getOptions(): array
33
    {
34 1
        $arrayOfRules = [];
35 1
        foreach ($this->rules as $rule) {
36 1
            if ($rule instanceof RuleInterface) {
37 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
38
            } else {
39
                $arrayOfRules[] = [get_class($rule)];
40
            }
41
        }
42 1
        return $arrayOfRules;
43
    }
44
}
45