GraphQL\Tests\Language\TestUtils::nodeToArray PHP Method

nodeToArray() public static method

public static nodeToArray ( Node $node ) : array
$node GraphQL\Language\AST\Node
return array
    public static function nodeToArray(Node $node)
    {
        $result = ['kind' => $node->kind, 'loc' => self::locationToArray($node->loc)];
        foreach (get_object_vars($node) as $prop => $propValue) {
            if (isset($result[$prop])) {
                continue;
            }
            if (is_array($propValue)) {
                $tmp = [];
                foreach ($propValue as $tmp1) {
                    $tmp[] = $tmp1 instanceof Node ? self::nodeToArray($tmp1) : (array) $tmp1;
                }
            } else {
                if ($propValue instanceof Node) {
                    $tmp = self::nodeToArray($propValue);
                } else {
                    if (is_scalar($propValue) || null === $propValue) {
                        $tmp = $propValue;
                    } else {
                        $tmp = null;
                    }
                }
            }
            $result[$prop] = $tmp;
        }
        return $result;
    }

Usage Example

Example #1
0
    /**
     * @it Simple input object
     */
    public function testSimpleInputObject()
    {
        $body = '
input Hello {
  world: String
}';
        $doc = Parser::parse($body);
        $loc = function ($start, $end) {
            return TestUtils::locArray($start, $end);
        };
        $expected = ['kind' => NodeKind::DOCUMENT, 'definitions' => [['kind' => NodeKind::INPUT_OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(7, 12)), 'directives' => [], 'fields' => [$this->inputValueNode($this->nameNode('world', $loc(17, 22)), $this->typeNode('String', $loc(24, 30)), null, $loc(17, 30))], 'loc' => $loc(1, 32)]], 'loc' => $loc(0, 32)];
        $this->assertEquals($expected, TestUtils::nodeToArray($doc));
    }
All Usage Examples Of GraphQL\Tests\Language\TestUtils::nodeToArray