1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Validator; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
9
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
10
|
|
|
use GraphQL\Type\Definition\Type; |
11
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ValidationNode. |
15
|
|
|
* |
16
|
|
|
* Holds the input data of the associated to it GraphQL type. Properties will be |
17
|
|
|
* created dinamically in runtime. In order to avoid name conflicts all built in |
18
|
|
|
* property names are prefixed with double underscores. |
19
|
|
|
* |
20
|
|
|
* It also contains variables of the resolver context, in which this class was |
21
|
|
|
* instantiated. |
22
|
|
|
*/ |
23
|
|
|
class ValidationNode |
24
|
|
|
{ |
25
|
|
|
private const KNOWN_VAR_NAMES = ['value', 'args', 'context', 'info']; |
26
|
|
|
|
27
|
|
|
private ?ValidationNode $__parent = null; |
28
|
|
|
private ?string $__fieldName; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ObjectType|InputObjectType|Type |
32
|
|
|
*/ |
33
|
|
|
private Type $__type; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ValidationNode[] |
37
|
|
|
*/ |
38
|
|
|
private array $__children = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Arguments of the resolver, where the current validation is being executed. |
42
|
|
|
*/ |
43
|
|
|
private array $__resolverArgs; |
44
|
|
|
|
45
|
18 |
|
public function __construct(Type $type, string $field = null, ?ValidationNode $parent = null, array $resolverArgs = []) |
46
|
|
|
{ |
47
|
18 |
|
$this->__type = $type; |
48
|
18 |
|
$this->__fieldName = $field; |
49
|
18 |
|
$this->__resolverArgs = $resolverArgs; |
50
|
|
|
|
51
|
18 |
|
if (null !== $parent) { |
52
|
7 |
|
$this->__parent = $parent; |
53
|
7 |
|
$parent->addChild($this); |
54
|
|
|
} |
55
|
18 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a GraphQL type associated to this object. |
59
|
|
|
* |
60
|
|
|
* @return ObjectType|InputObjectType|Type |
61
|
|
|
*/ |
62
|
1 |
|
public function getType(): Type |
63
|
|
|
{ |
64
|
1 |
|
return $this->__type; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the name of the associated GraphQL type. |
69
|
|
|
* Shortcut for `getType()->name`. |
70
|
|
|
*/ |
71
|
18 |
|
public function getName(): string |
72
|
|
|
{ |
73
|
18 |
|
return $this->__type->name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the field name of the type (only for root type). |
78
|
|
|
*/ |
79
|
1 |
|
public function getFieldName(): ?string |
80
|
|
|
{ |
81
|
1 |
|
return $this->__fieldName; |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
public function getParent(): ?ValidationNode |
85
|
|
|
{ |
86
|
7 |
|
return $this->__parent; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @internal |
91
|
|
|
*/ |
92
|
7 |
|
public function addChild(ValidationNode $child): void |
93
|
|
|
{ |
94
|
7 |
|
$this->__children[] = $child; |
95
|
7 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Traverses up through parent nodes and returns the first matching one. |
99
|
|
|
*/ |
100
|
1 |
|
public function findParent(string $name): ?ValidationNode |
101
|
|
|
{ |
102
|
1 |
|
$current = $this->__parent; |
103
|
|
|
|
104
|
1 |
|
while (null !== $current) { |
105
|
1 |
|
if ($current->getName() === $name) { |
106
|
1 |
|
return $current; |
107
|
|
|
} else { |
108
|
1 |
|
$current = $current->getParent(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns an argument of the resolver, where this validation is being executed. |
117
|
|
|
* |
118
|
|
|
* @return ResolveInfo|Argument|mixed|null |
119
|
|
|
*/ |
120
|
10 |
|
public function getResolverArg(string $name) |
121
|
|
|
{ |
122
|
10 |
|
if (\in_array($name, self::KNOWN_VAR_NAMES)) { |
123
|
10 |
|
return $this->__resolverArgs[$name]; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function __get($name) |
130
|
|
|
{ |
131
|
|
|
return $this->$name ?? null; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|