From 44abd5089f3adb455d50971525aad2f1d2f33c09 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 27 Jan 2022 02:05:07 +0100 Subject: [PATCH] Node::getSubNodes() changed to IteratorAggregate generator, --- src/Neon/Node.php | 12 +++++++----- src/Neon/Node/ArrayItemNode.php | 7 +++++-- src/Neon/Node/ArrayNode.php | 7 ++----- src/Neon/Node/EntityChainNode.php | 7 ++----- src/Neon/Node/EntityNode.php | 9 ++++----- src/Neon/Traverser.php | 2 +- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/Neon/Node.php b/src/Neon/Node.php index 4c9137b7..478b38f1 100644 --- a/src/Neon/Node.php +++ b/src/Neon/Node.php @@ -10,8 +10,10 @@ namespace Nette\Neon; -/** @internal */ -abstract class Node +/** + * @implements \IteratorAggregate + */ +abstract class Node implements \IteratorAggregate { /** @var ?int */ public $startTokenPos; @@ -33,9 +35,9 @@ abstract public function toValue(); abstract public function toString(): string; - /** @return self[] */ - public function getSubNodes(): array + public function &getIterator(): \Generator { - return []; + return; + yield; } } diff --git a/src/Neon/Node/ArrayItemNode.php b/src/Neon/Node/ArrayItemNode.php index da9fd32f..322219dd 100644 --- a/src/Neon/Node/ArrayItemNode.php +++ b/src/Neon/Node/ArrayItemNode.php @@ -80,8 +80,11 @@ public function toString(): string } - public function getSubNodes(): array + public function &getIterator(): \Generator { - return $this->key ? [&$this->key, &$this->value] : [&$this->value]; + if ($this->key) { + yield $this->key; + } + yield $this->value; } } diff --git a/src/Neon/Node/ArrayNode.php b/src/Neon/Node/ArrayNode.php index 4b83bb62..c46cdfea 100644 --- a/src/Neon/Node/ArrayNode.php +++ b/src/Neon/Node/ArrayNode.php @@ -25,13 +25,10 @@ public function toValue(): array } - public function getSubNodes(): array + public function &getIterator(): \Generator { - $res = []; foreach ($this->items as &$item) { - $res[] = &$item; + yield $item; } - - return $res; } } diff --git a/src/Neon/Node/EntityChainNode.php b/src/Neon/Node/EntityChainNode.php index a29a3fb9..b5654d03 100644 --- a/src/Neon/Node/EntityChainNode.php +++ b/src/Neon/Node/EntityChainNode.php @@ -43,13 +43,10 @@ public function toString(): string } - public function getSubNodes(): array + public function &getIterator(): \Generator { - $res = []; foreach ($this->chain as &$item) { - $res[] = &$item; + yield $item; } - - return $res; } } diff --git a/src/Neon/Node/EntityNode.php b/src/Neon/Node/EntityNode.php index eab456de..967016f2 100644 --- a/src/Neon/Node/EntityNode.php +++ b/src/Neon/Node/EntityNode.php @@ -48,13 +48,12 @@ public function toString(): string } - public function getSubNodes(): array + public function &getIterator(): \Generator { - $res = [&$this->value]; + yield $this->value; + foreach ($this->attributes as &$item) { - $res[] = &$item; + yield $item; } - - return $res; } } diff --git a/src/Neon/Traverser.php b/src/Neon/Traverser.php index 598e5873..73fcdb36 100644 --- a/src/Neon/Traverser.php +++ b/src/Neon/Traverser.php @@ -57,7 +57,7 @@ private function traverseNode(Node $node): Node } if ($children) { - foreach ($node->getSubNodes() as &$subnode) { + foreach ($node as &$subnode) { $subnode = $this->traverseNode($subnode); if ($this->stop) { break;