Passed
Pull Request — master (#300)
by Alexander
06:13 queued 03:06
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\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
14
use Yiisoft\Validator\RuleInterface;
15
use Yiisoft\Validator\SerializableRuleInterface;
16
use Yiisoft\Validator\SkipOnEmptyInterface;
17
use Yiisoft\Validator\ValidationContext;
18
19
/**
20
 * Validates an array by checking each of its elements against a set of rules.
21
 */
22
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
23
final class Each implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
24
{
25
    use BeforeValidationTrait;
26
    use RuleNameTrait;
27
    use SkipOnEmptyTrait;
28
29 3
    public function __construct(
30
        /**
31
         * @var iterable<RuleInterface>
32
         */
33
        private iterable $rules = [],
34
        private string $incorrectInputMessage = 'Value must be array or iterable.',
35
        private string $message = '{error} {value} given.',
36
37
        /**
38
         * @var bool|callable|null
39
         */
40
        private $skipOnEmpty = null,
41
        private bool $skipOnError = false,
42
        /**
43
         * @var Closure(mixed, ValidationContext):bool|null
44
         */
45
        private ?Closure $when = null,
46
    ) {
47
    }
48
49
    /**
50
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
51
     */
52 9
    public function getRules(): iterable
53
    {
54 9
        return $this->rules;
55
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    public function getIncorrectInputMessage(): string
61
    {
62 1
        return $this->incorrectInputMessage;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 8
    public function getMessage(): string
69
    {
70 8
        return $this->message;
71
    }
72
73 1
    #[ArrayShape([
74
        'incorrectInputMessage' => 'array',
75
        'message' => 'array',
76
        'skipOnEmpty' => 'bool',
77
        'skipOnError' => 'bool',
78
        'rules' => 'array',
79
    ])]
80
    public function getOptions(): array
81
    {
82 1
        $arrayOfRules = [];
83 1
        foreach ($this->rules as $rule) {
84 1
            if ($rule instanceof SerializableRuleInterface) {
85 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
86
            } else {
87
                $arrayOfRules[] = [$rule->getName()];
88
            }
89
        }
90
91
        return [
92
            'incorrectInputMessage' => [
93 1
                'message' => $this->getIncorrectInputMessage(),
94
            ],
95
            'message' => [
96 1
                'message' => $this->getMessage(),
97
            ],
98 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
99 1
            'skipOnError' => $this->skipOnError,
100
            'rules' => $arrayOfRules,
101
        ];
102
    }
103
104 6
    public function getHandlerClassName(): string
105
    {
106 6
        return EachHandler::class;
107
    }
108
}
109