Passed
Pull Request — master (#288)
by Alexander
10:50 queued 07:32
created

Each::getMessage()   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 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 $skipOnEmptyCallback = null,
35
        private bool $skipOnError = false,
36
        /**
37
         * @var Closure(mixed, ValidationContext):bool|null
38
         */
39
        private ?Closure $when = null,
40
    ) {
41 2
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
42
    }
43
44
    /**
45
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
46
     */
47 4
    public function getRules(): iterable
48
    {
49 4
        return $this->rules;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getIncorrectInputMessage(): string
56
    {
57 1
        return $this->incorrectInputMessage;
58
    }
59
60
    /**
61
     * @return string
62
     */
63 1
    public function getMessage(): string
64
    {
65 1
        return $this->message;
66
    }
67
68 1
    #[ArrayShape([
69
        'incorrectInputMessage' => 'array',
70
        'message' => 'array',
71
        'skipOnEmpty' => 'bool',
72
        'skipOnError' => 'bool',
73
        'rules' => 'array',
74
    ])]
75
    public function getOptions(): array
76
    {
77 1
        $arrayOfRules = [];
78 1
        foreach ($this->rules as $rule) {
79 1
            if ($rule instanceof SerializableRuleInterface) {
80 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
81
            } else {
82
                $arrayOfRules[] = [$rule->getName()];
83
            }
84
        }
85
86
        return [
87
            'incorrectInputMessage' => [
88 1
                'message' => $this->getIncorrectInputMessage(),
89
            ],
90
            'message' => [
91 1
                'message' => $this->getMessage(),
92
            ],
93 1
            'skipOnEmpty' => $this->skipOnEmpty,
94 1
            'skipOnError' => $this->skipOnError,
95
            'rules' => $arrayOfRules,
96
        ];
97
    }
98
99 1
    public function getHandlerClassName(): string
100
    {
101 1
        return EachHandler::class;
102
    }
103
}
104