1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Language\AST; |
6
|
|
|
|
7
|
|
|
use GraphQL\Utils\Utils; |
8
|
|
|
use function get_object_vars; |
9
|
|
|
use function is_array; |
10
|
|
|
use function is_scalar; |
11
|
|
|
use function json_encode; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* type Node = NameNode |
15
|
|
|
* | DocumentNode |
16
|
|
|
* | OperationDefinitionNode |
17
|
|
|
* | VariableDefinitionNode |
18
|
|
|
* | VariableNode |
19
|
|
|
* | SelectionSetNode |
20
|
|
|
* | FieldNode |
21
|
|
|
* | ArgumentNode |
22
|
|
|
* | FragmentSpreadNode |
23
|
|
|
* | InlineFragmentNode |
24
|
|
|
* | FragmentDefinitionNode |
25
|
|
|
* | IntValueNode |
26
|
|
|
* | FloatValueNode |
27
|
|
|
* | StringValueNode |
28
|
|
|
* | BooleanValueNode |
29
|
|
|
* | EnumValueNode |
30
|
|
|
* | ListValueNode |
31
|
|
|
* | ObjectValueNode |
32
|
|
|
* | ObjectFieldNode |
33
|
|
|
* | DirectiveNode |
34
|
|
|
* | ListTypeNode |
35
|
|
|
* | NonNullTypeNode |
36
|
|
|
*/ |
37
|
|
|
abstract class Node |
38
|
|
|
{ |
39
|
|
|
/** @var Location */ |
40
|
|
|
public $loc; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param (string|NameNode|NodeList|SelectionSetNode|Location|null)[] $vars |
44
|
|
|
*/ |
45
|
863 |
|
public function __construct(array $vars) |
46
|
|
|
{ |
47
|
863 |
|
if (empty($vars)) { |
48
|
9 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
861 |
|
Utils::assign($this, $vars); |
52
|
861 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
4 |
|
public function cloneDeep() |
58
|
|
|
{ |
59
|
4 |
|
return $this->cloneValue($this); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string|NodeList|Location|Node|(Node|NodeList|Location)[] $value |
64
|
|
|
* @return string|NodeList|Location|Node |
65
|
|
|
*/ |
66
|
4 |
|
private function cloneValue($value) |
67
|
|
|
{ |
68
|
4 |
|
if (is_array($value)) { |
69
|
|
|
$cloned = []; |
70
|
|
|
foreach ($value as $key => $arrValue) { |
71
|
|
|
$cloned[$key] = $this->cloneValue($arrValue); |
72
|
|
|
} |
73
|
4 |
|
} elseif ($value instanceof self) { |
74
|
4 |
|
$cloned = clone $value; |
75
|
4 |
|
foreach (get_object_vars($cloned) as $prop => $propValue) { |
76
|
4 |
|
$cloned->{$prop} = $this->cloneValue($propValue); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
4 |
|
$cloned = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
return $cloned; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function __toString() |
89
|
|
|
{ |
90
|
|
|
$tmp = $this->toArray(true); |
91
|
|
|
|
92
|
|
|
return (string) json_encode($tmp); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param bool $recursive |
97
|
|
|
* @return mixed[] |
98
|
|
|
*/ |
99
|
26 |
|
public function toArray($recursive = false) |
100
|
|
|
{ |
101
|
26 |
|
if ($recursive) { |
102
|
5 |
|
return $this->recursiveToArray($this); |
103
|
|
|
} |
104
|
|
|
|
105
|
21 |
|
$tmp = (array) $this; |
106
|
|
|
|
107
|
21 |
|
if ($this->loc) { |
108
|
9 |
|
$tmp['loc'] = [ |
109
|
9 |
|
'start' => $this->loc->start, |
110
|
9 |
|
'end' => $this->loc->end, |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
21 |
|
return $tmp; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return mixed[] |
119
|
|
|
*/ |
120
|
5 |
|
private function recursiveToArray(Node $node) |
121
|
|
|
{ |
122
|
|
|
$result = [ |
123
|
5 |
|
'kind' => $node->kind, |
124
|
|
|
]; |
125
|
|
|
|
126
|
5 |
|
if ($node->loc) { |
127
|
4 |
|
$result['loc'] = [ |
128
|
4 |
|
'start' => $node->loc->start, |
129
|
4 |
|
'end' => $node->loc->end, |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
5 |
|
foreach (get_object_vars($node) as $prop => $propValue) { |
134
|
5 |
|
if (isset($result[$prop])) { |
135
|
5 |
|
continue; |
136
|
|
|
} |
137
|
|
|
|
138
|
5 |
|
if ($propValue === null) { |
139
|
4 |
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
5 |
|
if (is_array($propValue) || $propValue instanceof NodeList) { |
143
|
5 |
|
$tmp = []; |
144
|
5 |
|
foreach ($propValue as $tmp1) { |
145
|
5 |
|
$tmp[] = $tmp1 instanceof Node ? $this->recursiveToArray($tmp1) : (array) $tmp1; |
146
|
|
|
} |
147
|
5 |
|
} elseif ($propValue instanceof Node) { |
148
|
5 |
|
$tmp = $this->recursiveToArray($propValue); |
149
|
5 |
|
} elseif (is_scalar($propValue) || $propValue === null) { |
150
|
5 |
|
$tmp = $propValue; |
151
|
|
|
} else { |
152
|
|
|
$tmp = null; |
153
|
|
|
} |
154
|
|
|
|
155
|
5 |
|
$result[$prop] = $tmp; |
156
|
|
|
} |
157
|
|
|
|
158
|
5 |
|
return $result; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|