mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 22:36:02 +03:00
Merge pull request #106 from AndreasHeiberg/fix-to-string-node
Fix __toString() for AST Nodes
This commit is contained in:
commit
5948d5198e
@ -80,12 +80,46 @@ abstract class Node
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$tmp = $this->toArray();
|
||||
$tmp['loc'] = [
|
||||
'start' => $this->loc->start,
|
||||
'end' => $this->loc->end
|
||||
];
|
||||
|
||||
return json_encode($tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$tmp = (array) $this;
|
||||
$tmp['loc'] = [
|
||||
'start' => $this->loc->start,
|
||||
'end' => $this->loc->end
|
||||
];
|
||||
return json_encode($tmp);
|
||||
|
||||
$this->recursiveToArray($tmp);
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
*/
|
||||
public function recursiveToArray(&$object)
|
||||
{
|
||||
if ($object instanceof Node) {
|
||||
/** @var Node $object */
|
||||
$object = $object->toArray();
|
||||
} elseif (is_object($object)) {
|
||||
$object = (array) $object;
|
||||
} elseif (is_array($object)) {
|
||||
foreach ($object as &$o) {
|
||||
$this->recursiveToArray($o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user