Passed
Push — master ( f0b656...d1524d )
by Alexander
02:42
created

Nested::propagateOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.9332
cc 4
nc 4
nop 0
crap 4
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 ReflectionProperty;
12
use Traversable;
13
use Yiisoft\Strings\StringHelper;
14
use Yiisoft\Validator\BeforeValidationInterface;
15
use Yiisoft\Validator\PropagateOptionsInterface;
16
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
17
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
18
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
19
use Yiisoft\Validator\RuleInterface;
20
use Yiisoft\Validator\RulesDumper;
21
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
22
use Yiisoft\Validator\RulesProviderInterface;
23
use Yiisoft\Validator\SerializableRuleInterface;
24
use Yiisoft\Validator\SkipOnEmptyInterface;
25
use Yiisoft\Validator\ValidationContext;
26
27
use function array_pop;
28
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...
29
use function implode;
30
use function is_array;
31
use function ltrim;
32
use function rtrim;
33
use function sprintf;
34
35
/**
36
 * Can be used for validation of nested structures.
37
 */
38
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
39
final class Nested implements
40
    SerializableRuleInterface,
41
    BeforeValidationInterface,
42
    SkipOnEmptyInterface,
43
    PropagateOptionsInterface
44
{
45
    use BeforeValidationTrait;
46
    use RuleNameTrait;
47
    use SkipOnEmptyTrait;
48
49
    private const SEPARATOR = '.';
50
    private const EACH_SHORTCUT = '*';
51
52
    /**
53
     * @var iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
54
     */
55
    private ?iterable $rules;
56
57 23
    public function __construct(
58
        /**
59
         * Rules for validate value that can be described by:
60
         * - object that implement {@see RulesProviderInterface};
61
         * - name of class from whose attributes their will be derived;
62
         * - array or object implementing the `Traversable` interface that contain {@see RuleInterface} implementations
63
         *   or closures.
64
         *
65
         * `$rules` can be null if validatable value is object. In this case rules will be derived from object via
66
         * `getRules()` method if object implement {@see RulesProviderInterface} or from attributes otherwise.
67
         *
68
         * @var class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null
69
         */
70
        iterable|object|string|null $rules = null,
71
72
        /**
73
         * @var int What visibility levels to use when reading data and rules from validated object.
74
         */
75
        private int $propertyVisibility = ReflectionProperty::IS_PRIVATE
76
        | ReflectionProperty::IS_PROTECTED
77
        | ReflectionProperty::IS_PUBLIC,
78
        /**
79
         * @var int What visibility levels to use when reading rules from the class specified in {@see $rules}
80
         * attribute.
81
         */
82
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
83
        | ReflectionProperty::IS_PROTECTED
84
        | ReflectionProperty::IS_PUBLIC,
85
        private bool $requirePropertyPath = false,
86
        private string $noPropertyPathMessage = 'Property path "{path}" is not found.',
87
        private bool $normalizeRules = true,
88
        private bool $propagateOptions = false,
89
90
        /**
91
         * @var bool|callable|null
92
         */
93
        private $skipOnEmpty = null,
94
        private bool $skipOnError = false,
95
96
        /**
97
         * @var Closure(mixed, ValidationContext):bool|null
98
         */
99
        private ?Closure $when = null,
100
    ) {
101 23
        $this->prepareRules($rules);
102
    }
103
104
    /**
105
     * @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
106
     */
107 39
    public function getRules(): ?iterable
108
    {
109 39
        return $this->rules;
110
    }
111
112 8
    public function getPropertyVisibility(): int
113
    {
114 8
        return $this->propertyVisibility;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120 33
    public function getRequirePropertyPath(): bool
121
    {
122 33
        return $this->requirePropertyPath;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 9
    public function getNoPropertyPathMessage(): string
129
    {
130 9
        return $this->noPropertyPathMessage;
131
    }
132
133
    /**
134
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null $source
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|iterable<Cl...sProviderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null.
Loading history...
135
     */
136 23
    private function prepareRules(iterable|object|string|null $source): void
137
    {
138 23
        if ($source === null) {
139 14
            $this->rules = null;
140
141 14
            return;
142
        }
143
144 9
        if ($source instanceof RulesProviderInterface) {
145 1
            $rules = $source->getRules();
146 8
        } elseif (!$source instanceof Traversable && !is_array($source)) {
147 4
            $rules = (new AttributesRulesProvider($source, $this->rulesPropertyVisibility))->getRules();
0 ignored issues
show
Bug introduced by
It seems like $source can also be of type iterable; however, parameter $source of Yiisoft\Validator\RulesP...Provider::__construct() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
            $rules = (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $source, $this->rulesPropertyVisibility))->getRules();
Loading history...
148
        } else {
149 4
            $rules = $source;
150
        }
151
152 9
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
153 9
        self::ensureArrayHasRules($rules);
154
155 8
        $this->rules = $rules;
156
157 8
        if ($this->normalizeRules) {
158 8
            $this->normalizeRules();
159
        }
160
161 7
        if ($this->propagateOptions) {
162 1
            $this->propagateOptions();
163
        }
164
    }
165
166 9
    private static function ensureArrayHasRules(iterable &$rules)
167
    {
168 9
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
169
170 9
        foreach ($rules as &$rule) {
171 8
            if (is_iterable($rule)) {
172 6
                self::ensureArrayHasRules($rule);
173 6
                continue;
174
            }
175 8
            if (!$rule instanceof RuleInterface) {
176 1
                $message = sprintf('Each rule should be an instance of %s, %s given.', RuleInterface::class, get_debug_type($rule));
177 1
                throw new InvalidArgumentException($message);
178
            }
179
        }
180
    }
181
182 8
    private function normalizeRules(): void
183
    {
184
        /** @var iterable $rules */
185 8
        $rules = $this->rules;
186
187 8
        while (true) {
188 8
            $breakWhile = true;
189 8
            $rulesMap = [];
190
191 8
            foreach ($rules as $valuePath => $rule) {
192 7
                if ($valuePath === self::EACH_SHORTCUT) {
193 1
                    throw new InvalidArgumentException('Bare shortcut is prohibited. Use "Each" rule instead.');
194
                }
195
196 6
                $parts = StringHelper::parsePath(
197 6
                    (string) $valuePath,
198
                    delimiter: self::EACH_SHORTCUT,
199
                    preserveDelimiterEscaping: true
200
                );
201 6
                if (count($parts) === 1) {
202 6
                    continue;
203
                }
204
205
                $breakWhile = false;
206
207
                $lastValuePath = array_pop($parts);
208
                $lastValuePath = ltrim($lastValuePath, '.');
209
                $lastValuePath = str_replace('\\' . self::EACH_SHORTCUT, self::EACH_SHORTCUT, $lastValuePath);
210
211
                $remainingValuePath = implode(self::EACH_SHORTCUT, $parts);
212
                $remainingValuePath = rtrim($remainingValuePath, self::SEPARATOR);
213
214
                if (!isset($rulesMap[$remainingValuePath])) {
215
                    $rulesMap[$remainingValuePath] = [];
216
                }
217
218
                $rulesMap[$remainingValuePath][$lastValuePath] = $rule;
219
                unset($rules[$valuePath]);
220
            }
221
222 7
            foreach ($rulesMap as $valuePath => $nestedRules) {
223
                $rules[$valuePath] = new Each([new self($nestedRules, normalizeRules: false)]);
224
            }
225
226 7
            if ($breakWhile === true) {
227 7
                break;
228
            }
229
        }
230
231 7
        $this->rules = $rules;
232
    }
233
234 1
    public function propagateOptions(): void
235
    {
236 1
        $rules = [];
237 1
        foreach ($this->rules as $attributeRulesIndex => $attributeRules) {
238 1
            foreach ($attributeRules as $attributeRule) {
239 1
                $attributeRule = $attributeRule->skipOnEmpty($this->skipOnEmpty);
240 1
                $attributeRule = $attributeRule->skipOnError($this->skipOnError);
241 1
                $attributeRule = $attributeRule->when($this->when);
242
243 1
                $rules[$attributeRulesIndex][] = $attributeRule;
244
245 1
                if ($attributeRule instanceof PropagateOptionsInterface) {
246 1
                    $attributeRule->propagateOptions();
247
                }
248
            }
249
        }
250
251 1
        $this->rules = $rules;
252
    }
253
254 5
    #[ArrayShape([
255
        'requirePropertyPath' => 'bool',
256
        'noPropertyPathMessage' => 'array',
257
        'skipOnEmpty' => 'bool',
258
        'skipOnError' => 'bool',
259
        'rules' => 'array|null',
260
    ])]
261
    public function getOptions(): array
262
    {
263
        return [
264 5
            'requirePropertyPath' => $this->getRequirePropertyPath(),
265
            'noPropertyPathMessage' => [
266 5
                'message' => $this->getNoPropertyPathMessage(),
267
            ],
268 5
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
269 5
            'skipOnError' => $this->skipOnError,
270 5
            'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
271
        ];
272
    }
273
274 24
    public function getHandlerClassName(): string
275
    {
276 24
        return NestedHandler::class;
277
    }
278
}
279