From 9e6d132d679c85ffc196e9fd62fa359df1b0c4eb Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Fri, 26 Aug 2022 17:04:41 +0100 Subject: [PATCH] Fix code needed for SonataPageBundle (#1100) --- src/Model/BaseBlock.php | 20 +++++++++++++++++++ src/Model/BlockInterface.php | 1 + .../RecursiveBlockIteratorIteratorTest.php | 3 +++ 3 files changed, 24 insertions(+) diff --git a/src/Model/BaseBlock.php b/src/Model/BaseBlock.php index 4bdce7a6..29c60fdb 100644 --- a/src/Model/BaseBlock.php +++ b/src/Model/BaseBlock.php @@ -199,6 +199,26 @@ public function addChildren(BlockInterface $children): void $children->setParent($this); } + public function removeChild(BlockInterface $child): void + { + // NEXT_MAJOR: Remove this condition, children will be a Collection. + if (\is_array($this->children)) { + $key = array_search($child, $this->children, true); + + if (false === $key) { + return; + } + + unset($this->children[$key]); + + return; + } + + if ($this->children->contains($child)) { + $this->children->removeElement($child); + } + } + public function getChildren() { return $this->children; diff --git a/src/Model/BlockInterface.php b/src/Model/BlockInterface.php index 8d833321..2a8b2311 100644 --- a/src/Model/BlockInterface.php +++ b/src/Model/BlockInterface.php @@ -17,6 +17,7 @@ /** * @method void addChild(BlockInterface $child) + * @method void removeChild(BlockInterface $child) * @method bool hasChild() */ interface BlockInterface diff --git a/tests/Util/RecursiveBlockIteratorIteratorTest.php b/tests/Util/RecursiveBlockIteratorIteratorTest.php index 83b0c4d7..d19326f5 100644 --- a/tests/Util/RecursiveBlockIteratorIteratorTest.php +++ b/tests/Util/RecursiveBlockIteratorIteratorTest.php @@ -28,6 +28,9 @@ public function testInterface(): void $block1->addChild($block2); $block1->addChild($block3); + $block1->removeChild($block3); + $block1->removeChild($block4); + $block1->addChild($block3); $i = new RecursiveBlockIteratorIterator([$block1, $block4]);