diff --git a/src/AbstractContainer.php b/src/AbstractContainer.php index 2506a59..ed3c213 100644 --- a/src/AbstractContainer.php +++ b/src/AbstractContainer.php @@ -193,11 +193,12 @@ public function getPages() /** * Removes the given page from the container * - * @param Page\AbstractPage|int $page page to remove, either a page - * instance or a specific page order + * @param Page\AbstractPage|int $page page to remove, either a page + * instance or a specific page order + * @param bool $recursive [optional] whether to remove recursively * @return bool whether the removal was successful */ - public function removePage($page) + public function removePage($page, $recursive = false) { if ($page instanceof Page\AbstractPage) { $hash = $page->hashCode(); @@ -217,6 +218,16 @@ public function removePage($page) return true; } + if ($recursive) { + /** @var \Zend\Navigation\Page\AbstractPage $childPage */ + foreach ($this->pages as $childPage) { + if ($childPage->hasPage($page, true)) { + $childPage->removePage($page, true); + return true; + } + } + } + return false; } diff --git a/test/ContainerTest.php b/test/ContainerTest.php index 7eb6046..b933d34 100644 --- a/test/ContainerTest.php +++ b/test/ContainerTest.php @@ -1080,4 +1080,31 @@ public function testGetChildrenShouldReturnNullWhenContainerIsEmpty() $this->assertEquals(null, $container->getChildren()); } + + /** + * @group GH-5929 + */ + public function testRemovePageRecursively() + { + $container = new Navigation\Navigation(array( + array( + 'route' => 'foo', + 'pages' => array( + array( + 'route' => 'bar', + 'pages' => array( + array( + 'route' => 'baz', + ), + ), + ) + ) + ), + )); + + $container->removePage($container->findOneBy('route', 'baz'), true); + $this->assertNull($container->findOneBy('route', 'baz')); + $container->removePage($container->findOneBy('route', 'bar'), true); + $this->assertNull($container->findOneBy('route', 'bar')); + } }