Просмотр исходного кода

Merge pull request #333 from froschdesign/features/navigation/remove-page-recursively

[Zend_Navigation] Remove a page recursively
Rob Allen 11 лет назад
Родитель
Сommit
c45efe13dc
2 измененных файлов с 39 добавлено и 5 удалено
  1. 15 5
      library/Zend/Navigation/Container.php
  2. 24 0
      tests/Zend/Navigation/ContainerTest.php

+ 15 - 5
library/Zend/Navigation/Container.php

@@ -200,12 +200,12 @@ abstract class Zend_Navigation_Container implements RecursiveIterator, Countable
     /**
      * Removes the given page from the container
      *
-     * @param  Zend_Navigation_Page|int $page  page to remove, either a page
-     *                                         instance or a specific page order
-     * @return bool                            whether the removal was
-     *                                         successful
+     * @param  Zend_Navigation_Page|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 Zend_Navigation_Page) {
             $hash = $page->hashCode();
@@ -225,6 +225,16 @@ abstract class Zend_Navigation_Container implements RecursiveIterator, Countable
             return true;
         }
 
+        if ($recursive) {
+            /** @var Zend_Navigation_Page $childPage */
+            foreach ($this->_pages as $childPage) {
+                if ($childPage->hasPage($page, true)) {
+                    $childPage->removePage($page, true);
+                    return true;
+                }
+            }
+        }
+
         return false;
     }
 

+ 24 - 0
tests/Zend/Navigation/ContainerTest.php

@@ -1260,4 +1260,28 @@ class Zend_Navigation_ContainerTest extends PHPUnit_Framework_TestCase
 
         $this->assertEquals(null, $container->getChildren());
     }
+
+    public function testRemovePageRecursively()
+    {
+        $container = new Zend_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'));
+    }
 }