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