Skip to content

Commit 085516b

Browse files
committed
Moved GraphQL\Language\AST\Node::fromArray to GraphQL\Utils\AST::fromArray
1 parent bd44475 commit 085516b

File tree

4 files changed

+75
-59
lines changed

4 files changed

+75
-59
lines changed

src/Language/AST/Node.php

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,6 @@ abstract class Node
3838
*/
3939
public $loc;
4040

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 Node
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-
9641
/**
9742
* @param array $vars
9843
*/

src/Language/AST/NodeList.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace GraphQL\Language\AST;
33

4+
use GraphQL\Utils\AST;
5+
46
/**
57
* Class NodeList
68
*
@@ -49,7 +51,7 @@ public function offsetGet($offset)
4951
$item = $this->nodes[$offset];
5052

5153
if (is_array($item) && isset($item['kind'])) {
52-
$this->nodes[$offset] = $item = Node::fromArray($item);
54+
$this->nodes[$offset] = $item = AST::fromArray($item);
5355
}
5456

5557
return $item;
@@ -62,7 +64,7 @@ public function offsetGet($offset)
6264
public function offsetSet($offset, $value)
6365
{
6466
if (is_array($value) && isset($value['kind'])) {
65-
$value = Node::fromArray($value);
67+
$value = AST::fromArray($value);
6668
}
6769
$this->nodes[$offset] = $value;
6870
}

src/Utils/AST.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
use GraphQL\Language\AST\BooleanValueNode;
66
use GraphQL\Language\AST\DocumentNode;
77
use GraphQL\Language\AST\EnumValueNode;
8-
use GraphQL\Language\AST\FieldNode;
98
use GraphQL\Language\AST\FloatValueNode;
109
use GraphQL\Language\AST\IntValueNode;
1110
use GraphQL\Language\AST\ListTypeNode;
1211
use GraphQL\Language\AST\ListValueNode;
12+
use GraphQL\Language\AST\Location;
1313
use GraphQL\Language\AST\NamedTypeNode;
1414
use GraphQL\Language\AST\NameNode;
1515
use GraphQL\Language\AST\Node;
16+
use GraphQL\Language\AST\NodeKind;
17+
use GraphQL\Language\AST\NodeList;
1618
use GraphQL\Language\AST\NonNullTypeNode;
1719
use GraphQL\Language\AST\NullValueNode;
1820
use GraphQL\Language\AST\ObjectFieldNode;
@@ -38,6 +40,72 @@
3840
*/
3941
class AST
4042
{
43+
/**
44+
* Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.
45+
*
46+
* For example:
47+
*
48+
* ```php
49+
* Node::fromArray([
50+
* 'kind' => 'ListValue',
51+
* 'values' => [
52+
* ['kind' => 'StringValue', 'value' => 'my str'],
53+
* ['kind' => 'StringValue', 'value' => 'my other str']
54+
* ],
55+
* 'loc' => ['start' => 21, 'end' => 25]
56+
* ]);
57+
* ```
58+
*
59+
* Will produce instance of `ListValueNode` where `values` prop is a lazily-evaluated `NodeList`
60+
* returning instances of `StringValueNode` on access.
61+
*
62+
* This is a reverse operation for $node->toArray(true)
63+
*
64+
* @param array $node
65+
* @return Node
66+
*/
67+
public static function fromArray(array $node)
68+
{
69+
if (!isset($node['kind']) || !isset(NodeKind::$classMap[$node['kind']])) {
70+
throw new InvariantViolation("Unexpected node structure: " . Utils::printSafeJson($node));
71+
}
72+
73+
$kind = isset($node['kind']) ? $node['kind'] : null;
74+
$class = NodeKind::$classMap[$kind];
75+
$instance = new $class([]);
76+
77+
if (isset($node['loc'], $node['loc']['start'], $node['loc']['end'])) {
78+
$instance->loc = Location::create($node['loc']['start'], $node['loc']['end']);
79+
}
80+
81+
82+
foreach ($node as $key => $value) {
83+
if ('loc' === $key || 'kind' === $key) {
84+
continue ;
85+
}
86+
if (is_array($value)) {
87+
if (isset($value[0]) || empty($value)) {
88+
$value = new NodeList($value);
89+
} else {
90+
$value = self::fromArray($value);
91+
}
92+
}
93+
$instance->{$key} = $value;
94+
}
95+
return $instance;
96+
}
97+
98+
/**
99+
* Convert AST node to serializable array
100+
*
101+
* @param Node $node
102+
* @return array
103+
*/
104+
public static function toArray(Node $node)
105+
{
106+
return $node->toArray(true);
107+
}
108+
41109
/**
42110
* Produces a GraphQL Value AST given a PHP value.
43111
*

tests/Language/SerializationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GraphQL\Language\AST\Node;
66
use GraphQL\Language\AST\NodeList;
77
use GraphQL\Language\Parser;
8+
use GraphQL\Utils\AST;
89

910
class SerializationTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -20,7 +21,7 @@ public function testUnserializesAst()
2021
{
2122
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
2223
$serializedAst = json_decode(file_get_contents(__DIR__ . '/kitchen-sink.ast'), true);
23-
$actualAst = Node::fromArray($serializedAst);
24+
$actualAst = AST::fromArray($serializedAst);
2425
$parsedAst = Parser::parse($kitchenSink);
2526
$this->assertNodesAreEqual($parsedAst, $actualAst);
2627
}

0 commit comments

Comments
 (0)