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