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

ZF-7602: Patch to add IteratorAggregate like behaviour to Zend_Paginator

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17630 44c647ce-9c0f-0410-b52a-842ac1e357ba
norm2782 16 лет назад
Родитель
Сommit
8da541262e
3 измененных файлов с 135 добавлено и 28 удалено
  1. 48 28
      library/Zend/Paginator.php
  2. 40 0
      library/Zend/Paginator/AdapterAggregate.php
  3. 47 0
      tests/Zend/PaginatorTest.php

+ 48 - 28
library/Zend/Paginator.php

@@ -261,40 +261,44 @@ class Zend_Paginator implements Countable, IteratorAggregate
     public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
                                    array $prefixPaths = null)
     {
-        if ($adapter == self::INTERNAL_ADAPTER) {
-            if (is_array($data)) {
-                $adapter = 'Array';
-            } else if ($data instanceof Zend_Db_Table_Select) {
-                $adapter = 'DbTableSelect';
-            } else if ($data instanceof Zend_Db_Select) {
-                $adapter = 'DbSelect';
-            } else if ($data instanceof Iterator) {
-                $adapter = 'Iterator';
-            } else if (is_integer($data)) {
-                $adapter = 'Null';
-            } else {
-                $type = (is_object($data)) ? get_class($data) : gettype($data);
+        if ($data instanceof Zend_Paginator_AdapterAggregate) {
+            return new self($data->getPaginatorAdapter());
+        } else {
+            if ($adapter == self::INTERNAL_ADAPTER) {
+                if (is_array($data)) {
+                    $adapter = 'Array';
+                } else if ($data instanceof Zend_Db_Table_Select) {
+                    $adapter = 'DbTableSelect';
+                } else if ($data instanceof Zend_Db_Select) {
+                    $adapter = 'DbSelect';
+                } else if ($data instanceof Iterator) {
+                    $adapter = 'Iterator';
+                } else if (is_integer($data)) {
+                    $adapter = 'Null';
+                } else {
+                    $type = (is_object($data)) ? get_class($data) : gettype($data);
 
-                /**
-                 * @see Zend_Paginator_Exception
-                 */
-                require_once 'Zend/Paginator/Exception.php';
+                    /**
+                     * @see Zend_Paginator_Exception
+                     */
+                    require_once 'Zend/Paginator/Exception.php';
 
-                throw new Zend_Paginator_Exception('No adapter for type ' . $type);
+                    throw new Zend_Paginator_Exception('No adapter for type ' . $type);
+                }
             }
-        }
 
-        $pluginLoader = self::getAdapterLoader();
+            $pluginLoader = self::getAdapterLoader();
 
-        if (null !== $prefixPaths) {
-            foreach ($prefixPaths as $prefix => $path) {
-                $pluginLoader->addPrefixPath($prefix, $path);
+            if (null !== $prefixPaths) {
+                foreach ($prefixPaths as $prefix => $path) {
+                    $pluginLoader->addPrefixPath($prefix, $path);
+                }
             }
-        }
 
-        $adapterClassName = $pluginLoader->load($adapter);
+            $adapterClassName = $pluginLoader->load($adapter);
 
-        return new self(new $adapterClassName($data));
+            return new self(new $adapterClassName($data));
+        }
     }
 
     /**
@@ -410,10 +414,26 @@ class Zend_Paginator implements Countable, IteratorAggregate
 
     /**
      * Constructor.
+     *
+     * @param Zend_Paginator_Adapter_Interface|Zend_Paginator_AdapterAggregate $adapter
      */
-    public function __construct(Zend_Paginator_Adapter_Interface $adapter)
+    public function __construct($adapter)
     {
-        $this->_adapter = $adapter;
+        if ($adapter instanceof Zend_Paginator_Adapter_Interface) {
+            $this->_adapter = $adapter;
+        } else if ($adapter instanceof Zend_Paginator_AdapterAggregate) {
+            $this->_adapter = $adapter->getPaginatorAdapter();
+        } else {
+            /**
+             * @see Zend_Paginator_Exception
+             */
+            require_once 'Zend/Paginator/Exception.php';
+
+            throw new Zend_Paginator_Exception(
+                'Zend_Paginator only accepts instances of the type ' .
+                'Zend_Paginator_Adapter_Interface or Zend_Paginator_AdapterAggregate.'
+            );
+        }
 
         $config = self::$_config;
 

+ 40 - 0
library/Zend/Paginator/AdapterAggregate.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Paginator
+ * @subpackage Adapter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * Interface that aggregates a Zend_Paginator_Adapter_Abstract just like IteratorAggregate does for Iterators.
+ *
+ * @category   Zend
+ * @package    Paginator
+ * @subpackage Adapter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+interface Zend_Paginator_AdapterAggregate
+{
+    /**
+     * Return a fully configured Paginator Adapter from this method.
+     *
+     * @return Zend_Paginator_Adapter_Abstract
+     */
+    public function getPaginatorAdapter();
+}

+ 47 - 0
tests/Zend/PaginatorTest.php

@@ -36,6 +36,11 @@ require_once dirname(__FILE__) . '/../TestHelper.php';
 require_once 'Zend/Paginator.php';
 
 /**
+ * @see Zend_Paginator_AdapterAggregate
+ */
+require_once 'Zend/Paginator/AdapterAggregate.php';
+
+/**
  * @see PHPUnit_Framework_TestCase
  */
 require_once 'PHPUnit/Framework/TestCase.php';
@@ -895,6 +900,48 @@ class Zend_PaginatorTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(9, $this->_paginator->getItem(-2, 1));
         $this->assertEquals(101, $this->_paginator->getItem(-1, -1));
     }
+
+    /**
+     * @group ZF-7602
+     */
+    public function testAcceptAndHandlePaginatorAdapterAggregateDataInFactory()
+    {
+        $p = Zend_Paginator::factory(new Zend_Paginator_TestArrayAggregate());
+
+        $this->assertEquals(1, count($p));
+        $this->assertType('Zend_Paginator_Adapter_Array', $p->getAdapter());
+        $this->assertEquals(4, count($p->getAdapter()));
+    }
+
+    /**
+     * @group ZF-7602
+     */
+    public function testAcceptAndHandlePaginatorAdapterAggreageInConstructor()
+    {
+        $p = new Zend_Paginator(new Zend_Paginator_TestArrayAggregate());
+
+        $this->assertEquals(1, count($p));
+        $this->assertType('Zend_Paginator_Adapter_Array', $p->getAdapter());
+        $this->assertEquals(4, count($p->getAdapter()));
+    }
+
+    /**
+     * @group ZF-7602
+     */
+    public function testInvalidDataInConstructor_ThrowsException()
+    {
+        $this->setExpectedException("Zend_Paginator_Exception");
+
+        $p = new Zend_Paginator(array());
+    }
+}
+
+class Zend_Paginator_TestArrayAggregate implements Zend_Paginator_AdapterAggregate
+{
+    public function getPaginatorAdapter()
+    {
+        return new Zend_Paginator_Adapter_Array(array(1, 2, 3, 4));
+    }
 }
 
 // Call Zend_PaginatorTest::main() if this source file is executed directly.