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