From ed8bf4e2b2ec23f44a8d5b33dba9e1329d484671 Mon Sep 17 00:00:00 2001 From: Andreas Heiberg Date: Fri, 7 Apr 2017 11:34:33 +0100 Subject: [PATCH] fix __toString() for AST Node previously it would only shallowly convert to array causing json_encode to fail --- src/Language/AST/Node.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Language/AST/Node.php b/src/Language/AST/Node.php index ce5a77b..fc3349c 100644 --- a/src/Language/AST/Node.php +++ b/src/Language/AST/Node.php @@ -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); + } + } } }