|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Schema Validator |
|
4
|
|
|
* |
|
5
|
|
|
* @author Vlad Shashkov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2021, The Myaza Software |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SchemaValidator\Validator; |
|
12
|
|
|
|
|
13
|
|
|
use SchemaValidator\Argument; |
|
14
|
|
|
use SchemaValidator\CollectionInfoExtractor\CollectionInfoExtractorInterface; |
|
15
|
|
|
use SchemaValidator\Context; |
|
16
|
|
|
use SchemaValidator\Schema; |
|
17
|
|
|
use Symfony\Component\Validator\Constraints\All; |
|
18
|
|
|
use Symfony\Component\Validator\Constraints\Type; |
|
19
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidator; |
|
20
|
|
|
|
|
21
|
|
|
final class ArrayItemValidator implements ValidatorInterface |
|
22
|
|
|
{ |
|
23
|
10 |
|
public function __construct( |
|
24
|
|
|
private SymfonyValidator $validator, |
|
25
|
|
|
private CollectionInfoExtractorInterface $extractor, |
|
26
|
|
|
) { |
|
27
|
10 |
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
public function support(\ReflectionType $type): bool |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $type instanceof \ReflectionNamedType && $type->isBuiltin() && 'array' === $type->getName(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
6 |
|
public function validate(Argument $argument, Context $context): void |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var class-string $type */ |
|
37
|
6 |
|
$type = $context->getRootType(); |
|
38
|
6 |
|
$argumentName = $argument->getName(); |
|
39
|
6 |
|
$valueType = $this->extractor->getValueType($type, $argumentName); |
|
40
|
|
|
|
|
41
|
6 |
|
if (null === $valueType->getType()) { |
|
42
|
1 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @var array<string,array<string,mixed>|int|string|float> $value */ |
|
46
|
5 |
|
$value = is_array($argument->getValueByArgumentName()) ? $argument->getValueByArgumentName() : []; |
|
47
|
5 |
|
$rootPath = $context->getRootPath(); |
|
48
|
5 |
|
$propertyPath = $argument->getName(); |
|
49
|
5 |
|
$validator = $this->validator->inContext($context->getExecution())->atPath($rootPath); |
|
50
|
|
|
|
|
51
|
5 |
|
if ($valueType->isBuiltin()) { |
|
52
|
2 |
|
$validator->validate($value, new All([ |
|
53
|
2 |
|
new Type($valueType->getType()), |
|
54
|
|
|
])); |
|
55
|
|
|
|
|
56
|
2 |
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
if ([] === $value || !array_is_list($value)) { |
|
60
|
2 |
|
$validator->validate($value, new Schema([ |
|
61
|
2 |
|
'type' => $valueType->getType(), |
|
62
|
2 |
|
'rootPath' => $propertyPath . '[]', |
|
63
|
2 |
|
'strictTypes' => $context->isStrictTypes(), |
|
64
|
|
|
])); |
|
65
|
|
|
|
|
66
|
2 |
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
foreach ($value as $key => $item) { |
|
70
|
1 |
|
$validator->validate(is_array($item) ? $item : [$item], new Schema([ |
|
71
|
1 |
|
'type' => $valueType->getType(), |
|
72
|
1 |
|
'rootPath' => $propertyPath . '[' . $key . ']', |
|
73
|
1 |
|
'strictTypes' => $context->isStrictTypes(), |
|
74
|
|
|
])); |
|
75
|
|
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|