|
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
|
|
|
/** |
|
28
|
|
|
* @var ObjectType|InputObjectType|Type |
|
29
|
|
|
*/ |
|
30
|
|
|
private $__type; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ValidationNode|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private $__parent; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ValidationNode[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $__children = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $__fieldName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Arguments of the resolver, where the current validation is being executed. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $__resolverArgs; |
|
53
|
|
|
|
|
54
|
19 |
|
public function __construct(Type $type, string $field = null, ?ValidationNode $parent = null, array $resolverArgs = []) |
|
55
|
|
|
{ |
|
56
|
19 |
|
$this->__type = $type; |
|
57
|
19 |
|
$this->__fieldName = $field; |
|
58
|
19 |
|
$this->__resolverArgs = $resolverArgs; |
|
59
|
|
|
|
|
60
|
19 |
|
if (null !== $parent) { |
|
61
|
7 |
|
$this->__parent = $parent; |
|
62
|
7 |
|
$parent->addChild($this); |
|
63
|
|
|
} |
|
64
|
19 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns a GraphQL type associated to this object. |
|
68
|
|
|
* |
|
69
|
|
|
* @return ObjectType|InputObjectType|Type |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function getType(): Type |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->__type; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Gets the name of the associated GraphQL type. |
|
78
|
|
|
* Shortcut for `getType()->name`. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
19 |
|
public function getName(): string |
|
83
|
|
|
{ |
|
84
|
19 |
|
return $this->__type->name; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the field name of the type (only for root type). |
|
89
|
|
|
* |
|
90
|
|
|
* @return string|null |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getFieldName(): ?string |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->__fieldName; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return ValidationNode|null |
|
99
|
|
|
*/ |
|
100
|
7 |
|
public function getParent(): ?ValidationNode |
|
101
|
|
|
{ |
|
102
|
7 |
|
return $this->__parent; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @internal |
|
107
|
|
|
* |
|
108
|
|
|
* @param ValidationNode $child |
|
109
|
|
|
*/ |
|
110
|
7 |
|
public function addChild(ValidationNode $child): void |
|
111
|
|
|
{ |
|
112
|
7 |
|
$this->__children[] = $child; |
|
113
|
7 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Traverses up through parent nodes and returns the first matching one. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* |
|
120
|
|
|
* @return ValidationNode|null |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function findParent(string $name): ?ValidationNode |
|
123
|
|
|
{ |
|
124
|
1 |
|
$current = $this->__parent; |
|
125
|
|
|
|
|
126
|
1 |
|
while (null !== $current) { |
|
127
|
1 |
|
if ($current->getName() === $name) { |
|
128
|
1 |
|
return $current; |
|
129
|
|
|
} else { |
|
130
|
1 |
|
$current = $current->getParent(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns an argument of the resolver, where this validation is being executed. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $name |
|
141
|
|
|
* |
|
142
|
|
|
* @return ResolveInfo|Argument|mixed|null |
|
143
|
|
|
*/ |
|
144
|
10 |
|
public function getResolverArg(string $name) |
|
145
|
|
|
{ |
|
146
|
10 |
|
if (\in_array($name, self::KNOWN_VAR_NAMES)) { |
|
147
|
10 |
|
return $this->__resolverArgs[$name]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function __get($name) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->$name ?? null; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|