|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Paginator |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Iterator.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Paginator_Adapter_Interface |
|
24 */ |
|
25 require_once 'Zend/Paginator/Adapter/Interface.php'; |
|
26 |
|
27 /** |
|
28 * @see Zend_Paginator_SerializableLimitIterator |
|
29 */ |
|
30 require_once 'Zend/Paginator/SerializableLimitIterator.php'; |
|
31 |
|
32 /** |
|
33 * @category Zend |
|
34 * @package Zend_Paginator |
|
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
36 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
37 */ |
|
38 class Zend_Paginator_Adapter_Iterator implements Zend_Paginator_Adapter_Interface |
|
39 { |
|
40 /** |
|
41 * Iterator which implements Countable |
|
42 * |
|
43 * @var Iterator |
|
44 */ |
|
45 protected $_iterator = null; |
|
46 |
|
47 /** |
|
48 * Item count |
|
49 * |
|
50 * @var integer |
|
51 */ |
|
52 protected $_count = null; |
|
53 |
|
54 /** |
|
55 * Constructor. |
|
56 * |
|
57 * @param Iterator $iterator Iterator to paginate |
|
58 * @throws Zend_Paginator_Exception |
|
59 */ |
|
60 public function __construct(Iterator $iterator) |
|
61 { |
|
62 if (!$iterator instanceof Countable) { |
|
63 /** |
|
64 * @see Zend_Paginator_Exception |
|
65 */ |
|
66 require_once 'Zend/Paginator/Exception.php'; |
|
67 |
|
68 throw new Zend_Paginator_Exception('Iterator must implement Countable'); |
|
69 } |
|
70 |
|
71 $this->_iterator = $iterator; |
|
72 $this->_count = count($iterator); |
|
73 } |
|
74 |
|
75 /** |
|
76 * Returns an iterator of items for a page, or an empty array. |
|
77 * |
|
78 * @param integer $offset Page offset |
|
79 * @param integer $itemCountPerPage Number of items per page |
|
80 * @return LimitIterator|array |
|
81 */ |
|
82 public function getItems($offset, $itemCountPerPage) |
|
83 { |
|
84 if ($this->_count == 0) { |
|
85 return array(); |
|
86 } |
|
87 |
|
88 // @link http://bugs.php.net/bug.php?id=49906 | ZF-8084 |
|
89 // return new LimitIterator($this->_iterator, $offset, $itemCountPerPage); |
|
90 return new Zend_Paginator_SerializableLimitIterator($this->_iterator, $offset, $itemCountPerPage); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Returns the total number of rows in the collection. |
|
95 * |
|
96 * @return integer |
|
97 */ |
|
98 public function count() |
|
99 { |
|
100 return $this->_count; |
|
101 } |
|
102 } |