|
1 | 1 | <?php |
2 | 2 | namespace GraphQL\Language\AST; |
3 | 3 |
|
| 4 | +use GraphQL\Error\InvariantViolation; |
4 | 5 | use GraphQL\Utils\Utils; |
5 | 6 |
|
6 | 7 | abstract class Node |
@@ -37,12 +38,69 @@ abstract class Node |
37 | 38 | */ |
38 | 39 | public $loc; |
39 | 40 |
|
| 41 | + /** |
| 42 | + * Converts representation of AST as associative array to Node instance. |
| 43 | + * |
| 44 | + * For example: |
| 45 | + * |
| 46 | + * ```php |
| 47 | + * Node::fromArray([ |
| 48 | + * 'kind' => 'ListValue', |
| 49 | + * 'values' => [ |
| 50 | + * ['kind' => 'StringValue', 'value' => 'my str'], |
| 51 | + * ['kind' => 'StringValue', 'value' => 'my other str'] |
| 52 | + * ], |
| 53 | + * 'loc' => ['start' => 21, 'end' => 25] |
| 54 | + * ]); |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * Will produce instance of `ListValueNode` where `values` prop is a lazily-evaluated `NodeList` |
| 58 | + * returning instances of `StringValueNode` on access. |
| 59 | + * |
| 60 | + * This is a reverse operation for $node->toArray(true) |
| 61 | + * |
| 62 | + * @param array $node |
| 63 | + * @return EnumValueDefinitionNode |
| 64 | + */ |
| 65 | + public static function fromArray(array $node) |
| 66 | + { |
| 67 | + if (!isset($node['kind']) || !isset(NodeKind::$classMap[$node['kind']])) { |
| 68 | + throw new InvariantViolation("Unexpected node structure: " . Utils::printSafeJson($node)); |
| 69 | + } |
| 70 | + |
| 71 | + $kind = isset($node['kind']) ? $node['kind'] : null; |
| 72 | + $class = NodeKind::$classMap[$kind]; |
| 73 | + $instance = new $class([]); |
| 74 | + |
| 75 | + if (isset($node['loc'], $node['loc']['start'], $node['loc']['end'])) { |
| 76 | + $instance->loc = Location::create($node['loc']['start'], $node['loc']['end']); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + foreach ($node as $key => $value) { |
| 81 | + if ('loc' === $key || 'kind' === $key) { |
| 82 | + continue ; |
| 83 | + } |
| 84 | + if (is_array($value)) { |
| 85 | + if (isset($value[0]) || empty($value)) { |
| 86 | + $value = new NodeList($value); |
| 87 | + } else { |
| 88 | + $value = self::fromArray($value); |
| 89 | + } |
| 90 | + } |
| 91 | + $instance->{$key} = $value; |
| 92 | + } |
| 93 | + return $instance; |
| 94 | + } |
| 95 | + |
40 | 96 | /** |
41 | 97 | * @param array $vars |
42 | 98 | */ |
43 | 99 | public function __construct(array $vars) |
44 | 100 | { |
45 | | - Utils::assign($this, $vars); |
| 101 | + if (!empty($vars)) { |
| 102 | + Utils::assign($this, $vars); |
| 103 | + } |
46 | 104 | } |
47 | 105 |
|
48 | 106 | /** |
@@ -91,34 +149,53 @@ public function __toString() |
91 | 149 | */ |
92 | 150 | public function toArray($recursive = false) |
93 | 151 | { |
94 | | - $tmp = (array) $this; |
| 152 | + if ($recursive) { |
| 153 | + return $this->recursiveToArray($this); |
| 154 | + } else { |
| 155 | + $tmp = (array) $this; |
95 | 156 |
|
96 | | - $tmp['loc'] = [ |
97 | | - 'start' => $this->loc->start, |
98 | | - 'end' => $this->loc->end |
99 | | - ]; |
| 157 | + $tmp['loc'] = [ |
| 158 | + 'start' => $this->loc->start, |
| 159 | + 'end' => $this->loc->end |
| 160 | + ]; |
100 | 161 |
|
101 | | - if ($recursive) { |
102 | | - $this->recursiveToArray($tmp); |
| 162 | + return $tmp; |
103 | 163 | } |
104 | | - |
105 | | - return $tmp; |
106 | 164 | } |
107 | 165 |
|
108 | 166 | /** |
109 | | - * @param $object |
| 167 | + * @param Node $node |
| 168 | + * @return array |
110 | 169 | */ |
111 | | - public function recursiveToArray(&$object) |
| 170 | + private function recursiveToArray(Node $node) |
112 | 171 | { |
113 | | - if ($object instanceof Node) { |
114 | | - /** @var Node $object */ |
115 | | - $object = $object->toArray(true); |
116 | | - } elseif (is_object($object)) { |
117 | | - $object = (array) $object; |
118 | | - } elseif (is_array($object)) { |
119 | | - foreach ($object as &$o) { |
120 | | - $this->recursiveToArray($o); |
| 172 | + $result = [ |
| 173 | + 'kind' => $node->kind, |
| 174 | + 'loc' => [ |
| 175 | + 'start' => $node->loc->start, |
| 176 | + 'end' => $node->loc->end |
| 177 | + ] |
| 178 | + ]; |
| 179 | + |
| 180 | + foreach (get_object_vars($node) as $prop => $propValue) { |
| 181 | + if (isset($result[$prop])) |
| 182 | + continue; |
| 183 | + |
| 184 | + if (is_array($propValue) || $propValue instanceof NodeList) { |
| 185 | + $tmp = []; |
| 186 | + foreach ($propValue as $tmp1) { |
| 187 | + $tmp[] = $tmp1 instanceof Node ? $this->recursiveToArray($tmp1) : (array) $tmp1; |
| 188 | + } |
| 189 | + } else if ($propValue instanceof Node) { |
| 190 | + $tmp = $this->recursiveToArray($propValue); |
| 191 | + } else if (is_scalar($propValue) || null === $propValue) { |
| 192 | + $tmp = $propValue; |
| 193 | + } else { |
| 194 | + $tmp = null; |
121 | 195 | } |
| 196 | + |
| 197 | + $result[$prop] = $tmp; |
122 | 198 | } |
| 199 | + return $result; |
123 | 200 | } |
124 | 201 | } |
0 commit comments