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\Context; |
15
|
|
|
use SchemaValidator\Schema; |
16
|
|
|
use Symfony\Component\Validator\Util\PropertyPath; |
17
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidator; |
18
|
|
|
|
19
|
|
|
final class ObjectValidator implements ValidatorInterface, PriorityInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* ObjectValidator constructor. |
23
|
|
|
* |
24
|
|
|
* @param SymfonyValidator $validator |
25
|
|
|
*/ |
26
|
8 |
|
public function __construct( |
27
|
|
|
private SymfonyValidator $validator, |
28
|
|
|
) { |
29
|
8 |
|
} |
30
|
|
|
|
31
|
3 |
|
public function support(\ReflectionType $type): bool |
32
|
|
|
{ |
33
|
3 |
|
return $type instanceof \ReflectionNamedType && !$type->isBuiltin(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @psalm-suppress MixedAssignment |
38
|
|
|
*/ |
39
|
5 |
|
public function validate(Argument $argument, Context $context): void |
40
|
|
|
{ |
41
|
5 |
|
$type = $argument->getType(); |
42
|
|
|
|
43
|
5 |
|
if (!$type instanceof \ReflectionNamedType) { |
44
|
1 |
|
throw new \InvalidArgumentException('Type expected:' . \ReflectionNamedType::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
$value = $argument->getValueByArgumentName(); |
48
|
|
|
|
49
|
4 |
|
$this->validator->inContext($context->getExecution()) |
50
|
4 |
|
->validate(is_array($value) ? $value : [$value], [ |
51
|
4 |
|
new Schema([ |
52
|
4 |
|
'type' => $type->getName(), |
53
|
4 |
|
'rootPath' => PropertyPath::append($context->getRootPath(), $argument->getName()), |
54
|
4 |
|
'strictTypes' => $context->isStrictTypes(), |
55
|
|
|
]), |
56
|
|
|
]) |
57
|
|
|
; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
1 |
|
public static function priority(): int |
61
|
|
|
{ |
62
|
1 |
|
return -1; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|