Skip to content

Commit

Permalink
Node::getSubNodes() changed to IteratorAggregate generator,
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 10, 2022
1 parent f7e5415 commit 44abd50
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
12 changes: 7 additions & 5 deletions src/Neon/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
namespace Nette\Neon;


/** @internal */
abstract class Node
/**
* @implements \IteratorAggregate<Node>
*/
abstract class Node implements \IteratorAggregate
{
/** @var ?int */
public $startTokenPos;
Expand All @@ -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;
}
}
7 changes: 5 additions & 2 deletions src/Neon/Node/ArrayItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 2 additions & 5 deletions src/Neon/Node/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 2 additions & 5 deletions src/Neon/Node/EntityChainNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 4 additions & 5 deletions src/Neon/Node/EntityNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Neon/Traverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 44abd50

Please sign in to comment.