File indexing completed on 2024-06-16 05:30:16

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Paginator
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Paginator_Adapter_Interface
0024  */
0025 // require_once 'Zend/Paginator/Adapter/Interface.php';
0026 
0027 /**
0028  * @see Zend_Paginator_SerializableLimitIterator
0029  */
0030 // require_once 'Zend/Paginator/SerializableLimitIterator.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Paginator
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Paginator_Adapter_Iterator implements Zend_Paginator_Adapter_Interface
0039 {
0040     /**
0041      * Iterator which implements Countable
0042      *
0043      * @var Iterator
0044      */
0045     protected $_iterator = null;
0046 
0047     /**
0048      * Item count
0049      *
0050      * @var integer
0051      */
0052     protected $_count = null;
0053 
0054     /**
0055      * Constructor.
0056      *
0057      * @param  Iterator $iterator Iterator to paginate
0058      * @throws Zend_Paginator_Exception
0059      */
0060     public function __construct(Iterator $iterator)
0061     {
0062         if (!$iterator instanceof Countable) {
0063             /**
0064              * @see Zend_Paginator_Exception
0065              */
0066             // require_once 'Zend/Paginator/Exception.php';
0067 
0068             throw new Zend_Paginator_Exception('Iterator must implement Countable');
0069         }
0070 
0071         $this->_iterator = $iterator;
0072         $this->_count = count($iterator);
0073     }
0074 
0075     /**
0076      * Returns an iterator of items for a page, or an empty array.
0077      *
0078      * @param  integer $offset Page offset
0079      * @param  integer $itemCountPerPage Number of items per page
0080      * @return LimitIterator|array
0081      */
0082     public function getItems($offset, $itemCountPerPage)
0083     {
0084         if ($this->_count == 0) {
0085             return array();
0086         }
0087 
0088         // @link http://bugs.php.net/bug.php?id=49906 | ZF-8084
0089         // return new LimitIterator($this->_iterator, $offset, $itemCountPerPage);
0090         return new Zend_Paginator_SerializableLimitIterator($this->_iterator, $offset, $itemCountPerPage);
0091     }
0092 
0093     /**
0094      * Returns the total number of rows in the collection.
0095      *
0096      * @return integer
0097      */
0098     public function count()
0099     {
0100         return $this->_count;
0101     }
0102 }