Test Failed
Pull Request — master (#222)
by Rustam
02:30
created

Each::validateValue()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.1867

Importance

Changes 0
Metric Value
cc 8
eloc 22
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 38
ccs 18
cts 21
cp 0.8571
crap 8.1867
rs 8.4444
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
    public function __construct(
24
        /**
25 6
         * @var iterable<RuleInterface>
26
         */
27
        private iterable $rules = [],
28
        private string $incorrectInputMessage = 'Value should be array or iterable.',
29
        private string $message = '{error} {value} given.',
30
        private bool $skipOnEmpty = false,
31
        private bool $skipOnError = false,
32
        private ?Closure $when = null,
33
    ) {
34 6
    }
35 6
36
    /**
37
     * @return iterable<RuleInterface>
38 6
     */
39
    public function getRules(): iterable
40
    {
41 3
        return $this->rules;
42
    }
43 3
44
    /**
45
     * @return string
46
     */
47 3
    public function getIncorrectInputMessage(): string
48 3
    {
49
        return $this->incorrectInputMessage;
50
    }
51
52
    /**
53
     * @return string
54 3
     */
55 3
    public function getMessage(): string
56 3
    {
57 2
        return $this->message;
58
    }
59
60 3
    /**
61 3
     * @return bool
62 3
     */
63 3
    public function isSkipOnEmpty(): bool
64
    {
65 1
        return $this->skipOnEmpty;
66 1
    }
67
68
    /**
69 3
     * @return bool
70 3
     */
71
    public function isSkipOnError(): bool
72
    {
73
        return $this->skipOnError;
74 3
    }
75
76
    /**
77
     * @return Closure|null
78 3
     */
79
    public function getWhen(): ?Closure
80
    {
81 2
        return $this->when;
82
    }
83 2
84
    public function getOptions(): array
85
    {
86
        $arrayOfRules = [];
87
        foreach ($this->rules as $rule) {
88
            if ($rule instanceof ParametrizedRuleInterface) {
89
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
90
            } else {
91
                $arrayOfRules[] = [$rule->getName()];
92
            }
93
        }
94
        return $arrayOfRules;
95
    }
96
}
97