Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Fixes zendframework/zendframework#5929 - Remove a page recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
froschdesign committed Apr 4, 2014
1 parent db8e36b commit c2a2ce2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
27 changes: 27 additions & 0 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit c2a2ce2

Please sign in to comment.