Explorar el Código

ZF-9815: Zend_Navigation allow Container::addPages() accept another Container in addition to array or Zend_Config

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24458 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic hace 14 años
padre
commit
c680e12527
Se han modificado 2 ficheros con 27 adiciones y 5 borrados
  1. 13 5
      library/Zend/Navigation/Container.php
  2. 14 0
      tests/Zend/Navigation/ContainerTest.php

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

@@ -143,9 +143,12 @@ abstract class Zend_Navigation_Container implements RecursiveIterator, Countable
     /**
      * Adds several pages at once
      *
-     * @param  array|Zend_Config $pages   pages to add
-     * @return Zend_Navigation_Container  fluent interface, returns self
-     * @throws Zend_Navigation_Exception  if $pages is not array or Zend_Config
+     * @param  array|Zend_Config|Zend_Navigation_Container  $pages  pages to add
+     * @return Zend_Navigation_Container                    fluent interface,
+     *                                                      returns self
+     * @throws Zend_Navigation_Exception                    if $pages is not 
+     *                                                      array, Zend_Config or
+     *                                                      Zend_Navigation_Container
      */
     public function addPages($pages)
     {
@@ -153,11 +156,16 @@ abstract class Zend_Navigation_Container implements RecursiveIterator, Countable
             $pages = $pages->toArray();
         }
 
+        if ($pages instanceof Zend_Navigation_Container) {
+            $pages = iterator_to_array($pages);
+        }
+
         if (!is_array($pages)) {
             require_once 'Zend/Navigation/Exception.php';
             throw new Zend_Navigation_Exception(
-                    'Invalid argument: $pages must be an array or an ' .
-                    'instance of Zend_Config');
+                    'Invalid argument: $pages must be an array, an ' .
+                    'instance of Zend_Config or an instance of ' .
+                    'Zend_Navigation_Container');
         }
 
         foreach ($pages as $page) {

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

@@ -367,6 +367,20 @@ class Zend_Navigation_ContainerTest extends PHPUnit_Framework_TestCase
                             'Expected 3 pages, found ' . count($nav));
     }
 
+    /**
+     * @group ZF-9815
+     */
+    public function testAddPagesShouldWorkWithNavigationContainer()
+    {        
+        $nav = new Zend_Navigation();
+        $nav->addPages($this->_getFindByNavigation());
+        
+        $this->assertEquals(3, count($nav),
+                            'Expected 3 pages, found ' . count($nav));
+        
+        $this->assertEquals($nav->toArray(), $this->_getFindByNavigation()->toArray());
+    }
+
     public function testAddPagesShouldThrowExceptionWhenGivenString()
     {
         $nav = new Zend_Navigation();