Passed
Push — master ( 8efd82...c644e1 )
by Alexander
02:39
created

Each::getOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

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