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