-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathCompositePropertyResolver.php
More file actions
58 lines (46 loc) · 1.76 KB
/
Copy pathCompositePropertyResolver.php
File metadata and controls
58 lines (46 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Validators\Attributes;
use ReflectionProperty;
use Respect\Validation\Validator;
use Respect\Validation\Validators\Attributes;
use function array_values;
use function in_array;
final class CompositePropertyResolver implements PropertyResolver
{
/** @var list<PropertyResolver> */
private readonly array $resolvers;
public function __construct(PropertyResolver ...$resolvers)
{
$this->resolvers = array_values($resolvers);
}
/** @return array<Validator> */
public function resolve(ReflectionProperty $property, Attributes $attributes): array
{
$accumulated = [];
foreach ($this->resolvers as $resolver) {
$validators = $resolver->resolve($property, $attributes);
if ($validators === []) {
continue;
}
// When more than one resolver recognizes the same property (e.g. a
// class-typed property annotated with #[Attributes], which both
// DeclaredTypePropertyResolver and ExplicitAttributePropertyResolver
// emit `$attributes` for), collapse duplicate `$attributes` entries
// so the nested Attributes validator runs once instead of tripping
// its circular-reference guard on the second pass.
foreach ($validators as $validator) {
if ($validator === $attributes && in_array($validator, $accumulated, true)) {
continue;
}
$accumulated[] = $validator;
}
}
return $accumulated;
}
}