graphql-php/tests/Language/TestUtils.php

69 lines
1.6 KiB
PHP
Raw Permalink Normal View History

<?php
2018-09-01 21:21:08 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Language;
use GraphQL\Language\AST\Location;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeList;
2018-09-01 21:21:08 +03:00
use function get_object_vars;
use function is_array;
use function is_scalar;
class TestUtils
{
/**
2018-09-01 21:21:08 +03:00
* @return mixed[]
*/
2018-09-01 21:21:08 +03:00
public static function nodeToArray(Node $node) : array
{
$result = [
'kind' => $node->kind,
2018-09-01 21:21:08 +03:00
'loc' => self::locationToArray($node->loc),
];
foreach (get_object_vars($node) as $prop => $propValue) {
2018-09-01 21:21:08 +03:00
if (isset($result[$prop])) {
continue;
2018-09-01 21:21:08 +03:00
}
if (is_array($propValue) || $propValue instanceof NodeList) {
$tmp = [];
foreach ($propValue as $tmp1) {
$tmp[] = $tmp1 instanceof Node ? self::nodeToArray($tmp1) : (array) $tmp1;
}
2018-09-01 21:21:08 +03:00
} elseif ($propValue instanceof Node) {
$tmp = self::nodeToArray($propValue);
2018-09-01 21:21:08 +03:00
} elseif (is_scalar($propValue) || $propValue === null) {
$tmp = $propValue;
} else {
$tmp = null;
}
$result[$prop] = $tmp;
}
2018-09-01 21:21:08 +03:00
return $result;
}
/**
2018-09-01 21:21:08 +03:00
* @return int[]
*/
2018-09-01 21:21:08 +03:00
public static function locationToArray(Location $loc) : array
{
return [
'start' => $loc->start,
2018-09-01 21:21:08 +03:00
'end' => $loc->end,
];
}
/**
2018-09-01 21:21:08 +03:00
* @return int[]
*/
2018-09-01 21:21:08 +03:00
public static function locArray(int $start, int $end) : array
{
return ['start' => $start, 'end' => $end];
}
}