Passed
Pull Request — master (#294)
by Sergei
12:13
created

Nested::checkRules()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use InvalidArgumentException;
10
use JetBrains\PhpStorm\ArrayShape;
11
use Traversable;
12
use Yiisoft\Strings\StringHelper;
13
use Yiisoft\Validator\BeforeValidationInterface;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\RuleInterface;
17
use Yiisoft\Validator\RulesDumper;
18
use Yiisoft\Validator\SerializableRuleInterface;
19
use Yiisoft\Validator\ValidationContext;
20
21
use function array_pop;
22
use function count;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Validator\Rule\count. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use function implode;
24
use function is_array;
25
use function ltrim;
26
use function rtrim;
27
use function sprintf;
28
29
/**
30
 * Can be used for validation of nested structures.
31
 */
32
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
33
final class Nested implements SerializableRuleInterface, BeforeValidationInterface
34
{
35
    use BeforeValidationTrait;
36
    use RuleNameTrait;
37
38
    private const SEPARATOR = '.';
39
    private const EACH_SHORTCUT = '*';
40
41 7
    public function __construct(
42
        /**
43
         * @var iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
44
         */
45
        private ?iterable $rules = null,
46
        private bool $requirePropertyPath = false,
47
        private string $noPropertyPathMessage = 'Property path "{path}" is not found.',
48
        private bool $normalizeRules = true,
49
        private bool $skipOnEmpty = false,
50
        /**
51
         * @var callable
52
         */
53
        private $skipOnEmptyCallback = null,
54
        private bool $skipOnError = false,
55
        /**
56
         * @var Closure(mixed, ValidationContext):bool|null
57
         */
58
        private ?Closure $when = null,
59
    ) {
60 7
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
61
62 7
        if ($this->rules === null) {
63 1
            return;
64
        }
65
66 6
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
67 6
        if (empty($rules)) {
68 1
            throw new InvalidArgumentException('Rules must not be empty.');
69
        }
70
71 5
        if (self::checkRules($rules)) {
72 1
            $message = sprintf('Each rule should be an instance of %s.', RuleInterface::class);
73 1
            throw new InvalidArgumentException($message);
74
        }
75
76 4
        $this->rules = $rules;
77
78 4
        if ($this->normalizeRules === true) {
79 4
            $this->normalizeRules();
80
        }
81
    }
82
83
    /**
84
     * @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
85
     */
86 28
    public function getRules(): ?iterable
87
    {
88 28
        return $this->rules;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 27
    public function getRequirePropertyPath(): bool
95
    {
96 27
        return $this->requirePropertyPath;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 7
    public function getNoPropertyPathMessage(): string
103
    {
104 7
        return $this->noPropertyPathMessage;
105
    }
106
107 5
    private static function checkRules($rules): bool
108
    {
109 5
        return array_reduce(
110
            $rules,
111 5
            static function (bool $carry, $rule) {
112 5
                return $carry || (is_array($rule) ? self::checkRules($rule) : !$rule instanceof RuleInterface);
113
            },
114
            false
115
        );
116
    }
117
118 4
    private function normalizeRules(): void
119
    {
120
        /** @var iterable $rules */
121 4
        $rules = $this->getRules();
122 4
        while (true) {
123 4
            $breakWhile = true;
124 4
            $rulesMap = [];
125
126 4
            foreach ($rules as $valuePath => $rule) {
127 4
                if ($valuePath === self::EACH_SHORTCUT) {
128 1
                    throw new InvalidArgumentException('Bare shortcut is prohibited. Use "Each" rule instead.');
129
                }
130
131 3
                $parts = StringHelper::parsePath(
132 3
                    (string) $valuePath,
133
                    delimiter: self::EACH_SHORTCUT,
134
                    preserveDelimiterEscaping: true
135
                );
136 3
                if (count($parts) === 1) {
137 3
                    continue;
138
                }
139
140
                $breakWhile = false;
141
142
                $lastValuePath = array_pop($parts);
143
                $lastValuePath = ltrim($lastValuePath, '.');
144
                $lastValuePath = str_replace('\\' . self::EACH_SHORTCUT, self::EACH_SHORTCUT, $lastValuePath);
145
146
                $remainingValuePath = implode(self::EACH_SHORTCUT, $parts);
147
                $remainingValuePath = rtrim($remainingValuePath, self::SEPARATOR);
148
149
                if (!isset($rulesMap[$remainingValuePath])) {
150
                    $rulesMap[$remainingValuePath] = [];
151
                }
152
153
                $rulesMap[$remainingValuePath][$lastValuePath] = $rule;
154
                unset($rules[$valuePath]);
155
            }
156
157 3
            foreach ($rulesMap as $valuePath => $nestedRules) {
158
                $rules[$valuePath] = new Each([new self($nestedRules, normalizeRules: false)]);
159
            }
160
161 3
            if ($breakWhile === true) {
162 3
                break;
163
            }
164
        }
165
166 3
        $this->rules = $rules;
167
    }
168
169 4
    #[ArrayShape([
170
        'requirePropertyPath' => 'bool',
171
        'noPropertyPathMessage' => 'array',
172
        'skipOnEmpty' => 'bool',
173
        'skipOnError' => 'bool',
174
        'rules' => 'array|null',
175
    ])]
176
    public function getOptions(): array
177
    {
178
        return [
179 4
            'requirePropertyPath' => $this->getRequirePropertyPath(),
180
            'noPropertyPathMessage' => [
181 4
                'message' => $this->getNoPropertyPathMessage(),
182
            ],
183 4
            'skipOnEmpty' => $this->skipOnEmpty,
184 4
            'skipOnError' => $this->skipOnError,
185 4
            'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
186
        ];
187
    }
188
189 12
    public function getHandlerClassName(): string
190
    {
191 12
        return NestedHandler::class;
192
    }
193
}
194