Passed
Push — master ( cd2db1...fdabae )
by Alexander
02:29
created

Each::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\SerializableRuleInterface;
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 | Attribute::IS_REPEATABLE)]
21
final class Each implements SerializableRuleInterface, BeforeValidationInterface
22
{
23
    use BeforeValidationTrait;
24
    use RuleNameTrait;
25
26 3
    public function __construct(
27
        /**
28
         * @var iterable<RuleInterface>
29
         */
30
        private iterable $rules = [],
31
        private string $incorrectInputMessage = 'Value must be array or iterable.',
32
        private string $message = '{error} {value} given.',
33
        private bool $skipOnEmpty = false,
34
        /**
35
         * @var callable
36
         */
37
        private $skipOnEmptyCallback = null,
38
        private bool $skipOnError = false,
39
        /**
40
         * @var Closure(mixed, ValidationContext):bool|null
41
         */
42
        private ?Closure $when = null,
43
    ) {
44 3
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
45
    }
46
47
    /**
48
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
49
     */
50 8
    public function getRules(): iterable
51
    {
52 8
        return $this->rules;
53
    }
54
55
    /**
56
     * @return string
57
     */
58 1
    public function getIncorrectInputMessage(): string
59
    {
60 1
        return $this->incorrectInputMessage;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 8
    public function getMessage(): string
67
    {
68 8
        return $this->message;
69
    }
70
71 1
    #[ArrayShape([
72
        'incorrectInputMessage' => 'array',
73
        'message' => 'array',
74
        'skipOnEmpty' => 'bool',
75
        'skipOnError' => 'bool',
76
        'rules' => 'array',
77
    ])]
78
    public function getOptions(): array
79
    {
80 1
        $arrayOfRules = [];
81 1
        foreach ($this->rules as $rule) {
82 1
            if ($rule instanceof SerializableRuleInterface) {
83 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
84
            } else {
85
                $arrayOfRules[] = [$rule->getName()];
86
            }
87
        }
88
89
        return [
90
            'incorrectInputMessage' => [
91 1
                'message' => $this->getIncorrectInputMessage(),
92
            ],
93
            'message' => [
94 1
                'message' => $this->getMessage(),
95
            ],
96 1
            'skipOnEmpty' => $this->skipOnEmpty,
97 1
            'skipOnError' => $this->skipOnError,
98
            'rules' => $arrayOfRules,
99
        ];
100
    }
101
102 5
    public function getHandlerClassName(): string
103
    {
104 5
        return EachHandler::class;
105
    }
106
}
107