From 170ed5f533bd94c1e178b4b1feff3688d94ac3e1 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 5 Jun 2019 17:43:31 +0200 Subject: [PATCH] Now NodeInterface extends from IteratorAggregate and Traversable. --- src/Node/Node.php | 14 +++++++++++--- src/Node/NodeInterface.php | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Node/Node.php b/src/Node/Node.php index 0c792fa..44dd366 100644 --- a/src/Node/Node.php +++ b/src/Node/Node.php @@ -94,6 +94,14 @@ public function getAncestors(): \Traversable } } + /** + * {@inheritdoc} + */ + public function getIterator() + { + yield from $this->children(); + } + /** * {@inheritdoc} */ @@ -194,10 +202,10 @@ public function offsetUnset($offset) */ public function remove(NodeInterface ...$nodes): NodeInterface { - $this->storage['children'] = new \ArrayObject( + $this->storage['children']->exchangeArray( \array_filter( - (array) $this->storage['children'], - function ($child) use ($nodes) { + $this->storage['children']->getArrayCopy(), + static function ($child) use ($nodes) { return !\in_array($child, $nodes, true); } ) diff --git a/src/Node/NodeInterface.php b/src/Node/NodeInterface.php index 2b7c35e..dc43453 100644 --- a/src/Node/NodeInterface.php +++ b/src/Node/NodeInterface.php @@ -7,7 +7,7 @@ /** * Interface NodeInterface. */ -interface NodeInterface extends \Countable, \ArrayAccess +interface NodeInterface extends \Countable, \ArrayAccess, \Traversable, \IteratorAggregate { /** * The node to add.