Passed
Pull Request — master (#222)
by Rustam
02:28
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\ParametrizedRuleInterface;
10
use Yiisoft\Validator\PreValidatableRuleInterface;
11
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 11 at column 27
Loading history...
12
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\RuleInterface;
15
use Yiisoft\Validator\ValidationContext;
16
17
/**
18
 * Validates an array by checking each of its elements against a set of rules.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class Each implements ParametrizedRuleInterface, PreValidatableRuleInterface
22
{
23
    use HandlerClassNameTrait;
24
    use PreValidatableTrait;
25
    use RuleNameTrait;
26
27 2
    public function __construct(
28
        /**
29
         * @var iterable<RuleInterface>
30
         */
31
        private iterable $rules = [],
32
        private string $incorrectInputMessage = 'Value should be array or iterable.',
33
        private string $message = '{error} {value} given.',
34
        private bool $skipOnEmpty = false,
35
        private bool $skipOnError = false,
36
        /**
37
         * @var Closure(mixed, ValidationContext):bool|null
38
         */
39
        private ?Closure $when = null,
40
    ) {
41
    }
42
43
    /**
44
     * @return iterable<RuleInterface>
45
     */
46 3
    public function getRules(): iterable
47
    {
48 3
        return $this->rules;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getIncorrectInputMessage(): string
55
    {
56
        return $this->incorrectInputMessage;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getMessage(): string
63
    {
64
        return $this->message;
65
    }
66
67 1
    public function getOptions(): array
68
    {
69 1
        $arrayOfRules = [];
70 1
        foreach ($this->rules as $rule) {
71 1
            if ($rule instanceof ParametrizedRuleInterface) {
72 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
73
            } else {
74
                $arrayOfRules[] = [$rule->getName()];
75
            }
76
        }
77 1
        return $arrayOfRules;
78
    }
79
}
80