1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language\AST\Visitor; |
4
|
|
|
|
5
|
|
|
trait AcceptVisitorTrait |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
protected static $kindToNodesToVisitMap = [ |
9
|
|
|
'Name' => [], |
10
|
|
|
|
11
|
|
|
'Document' => ['definitions'], |
12
|
|
|
'OperationDefinition' => [ |
13
|
|
|
'name', |
14
|
|
|
'variableDefinitions', |
15
|
|
|
'directives', |
16
|
|
|
'selectionSet', |
17
|
|
|
], |
18
|
|
|
'VariableDefinition' => ['variable', 'type', 'defaultValue'], |
19
|
|
|
'Variable' => ['name'], |
20
|
|
|
'SelectionSet' => ['selections'], |
21
|
|
|
'Field' => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], |
22
|
|
|
'Argument' => ['name', 'value'], |
23
|
|
|
|
24
|
|
|
'FragmentSpread' => ['name', 'directives'], |
25
|
|
|
'InlineFragment' => ['typeCondition', 'directives', 'selectionSet'], |
26
|
|
|
'FragmentDefinition' => [ |
27
|
|
|
'name', |
28
|
|
|
// Note: fragment variable definitions are experimental and may be changed |
29
|
|
|
// or removed in the future. |
30
|
|
|
'variableDefinitions', |
31
|
|
|
'typeCondition', |
32
|
|
|
'directives', |
33
|
|
|
'selectionSet', |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
'IntValue' => [], |
37
|
|
|
'FloatValue' => [], |
38
|
|
|
'StringValue' => [], |
39
|
|
|
'BooleanValue' => [], |
40
|
|
|
'NullValue' => [], |
41
|
|
|
'EnumValue' => [], |
42
|
|
|
'ListValue' => ['values'], |
43
|
|
|
'ObjectValue' => ['fields'], |
44
|
|
|
'ObjectField' => ['name', 'value'], |
45
|
|
|
|
46
|
|
|
'Directive' => ['name', 'arguments'], |
47
|
|
|
|
48
|
|
|
'NamedType' => ['name'], |
49
|
|
|
'ListType' => ['type'], |
50
|
|
|
'NonNullType' => ['type'], |
51
|
|
|
|
52
|
|
|
'SchemaDefinition' => ['directives', 'operationTypes'], |
53
|
|
|
'OperationTypeDefinition' => ['type'], |
54
|
|
|
|
55
|
|
|
'ScalarTypeDefinition' => ['description', 'name', 'directives'], |
56
|
|
|
'ObjectTypeDefinition' => [ |
57
|
|
|
'description', |
58
|
|
|
'name', |
59
|
|
|
'interfaces', |
60
|
|
|
'directives', |
61
|
|
|
'fields', |
62
|
|
|
], |
63
|
|
|
'FieldDefinition' => ['description', 'name', 'arguments', 'type', 'directives'], |
64
|
|
|
'InputValueDefinition' => [ |
65
|
|
|
'description', |
66
|
|
|
'name', |
67
|
|
|
'type', |
68
|
|
|
'defaultValue', |
69
|
|
|
'directives', |
70
|
|
|
], |
71
|
|
|
'InterfaceTypeDefinition' => ['description', 'name', 'directives', 'fields'], |
72
|
|
|
'UnionTypeDefinition' => ['description', 'name', 'directives', 'types'], |
73
|
|
|
'EnumTypeDefinition' => ['description', 'name', 'directives', 'values'], |
74
|
|
|
'EnumValueDefinition' => ['description', 'name', 'directives'], |
75
|
|
|
'InputObjectTypeDefinition' => ['description', 'name', 'directives', 'fields'], |
76
|
|
|
|
77
|
|
|
'ScalarTypeExtension' => ['name', 'directives'], |
78
|
|
|
'ObjectTypeExtension' => ['name', 'interfaces', 'directives', 'fields'], |
79
|
|
|
'InterfaceTypeExtension' => ['name', 'directives', 'fields'], |
80
|
|
|
'UnionTypeExtension' => ['name', 'directives', 'types'], |
81
|
|
|
'EnumTypeExtension' => ['name', 'directives', 'values'], |
82
|
|
|
'InputObjectTypeExtension' => ['name', 'directives', 'fields'], |
83
|
|
|
|
84
|
|
|
'DirectiveDefinition' => ['description', 'name', 'arguments', 'locations'], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
abstract public function getKind(): string; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param VisitorInterface $visitor |
94
|
|
|
* @param string|null $key |
95
|
|
|
* @return array|null |
96
|
|
|
*/ |
97
|
|
|
public function accept(VisitorInterface $visitor, ?string $key = null, array $path = []): ?array |
98
|
|
|
{ |
99
|
|
|
/** @noinspection PhpParamsInspection */ |
100
|
|
|
if (null === ($enterResult = $visitor->enterNode($this, $key, $path))) { |
|
|
|
|
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$edited = $enterResult; |
105
|
|
|
foreach (self::$kindToNodesToVisitMap[$this->getKind()] as $name) { |
106
|
|
|
unset($edited[$name]); |
107
|
|
|
$value = $this->{$name}; |
108
|
|
|
if (\is_array($value) && !empty($value)) { |
109
|
|
|
$path[] = $name; |
110
|
|
|
$i = 0; |
111
|
|
|
foreach ($value as $v) { |
112
|
|
|
if ($v instanceof AcceptVisitorInterface) { |
113
|
|
|
$path[] = $i; |
114
|
|
|
if (null !== ($result = $v->accept($visitor, $i, $path))) { |
115
|
|
|
$edited[$name][$i] = $result; |
116
|
|
|
$i++; |
117
|
|
|
} |
118
|
|
|
$path = \array_slice($path, 0, count($path) - 1); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
$path = \array_slice($path, 0, count($path) - 1); |
122
|
|
|
} elseif ($value instanceof AcceptVisitorInterface) { |
123
|
|
|
$path[] = $name; |
124
|
|
|
if (null !== ($result = $value->accept($visitor, $name, $path))) { |
125
|
|
|
$edited[$name] = $result; |
126
|
|
|
} |
127
|
|
|
$path = \array_slice($path, 0, count($path) - 1); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @noinspection PhpParamsInspection */ |
132
|
|
|
if (null === ($leaveResult = $visitor->leaveNode($this, $key, $path))) { |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return array_merge($leaveResult, \is_array($edited) ? $edited : []); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|