Iterator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Paginator_Adapter_Interface
  23. */
  24. require_once 'Zend/Paginator/Adapter/Interface.php';
  25. /**
  26. * @see Zend_Paginator_SerializableLimitIterator
  27. */
  28. require_once 'Zend/Paginator/SerializableLimitIterator.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Paginator
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Paginator_Adapter_Iterator implements Zend_Paginator_Adapter_Interface
  36. {
  37. /**
  38. * Iterator which implements Countable
  39. *
  40. * @var Iterator
  41. */
  42. protected $_iterator = null;
  43. /**
  44. * Item count
  45. *
  46. * @var integer
  47. */
  48. protected $_count = null;
  49. /**
  50. * Constructor.
  51. *
  52. * @param Iterator $iterator Iterator to paginate
  53. * @throws Zend_Paginator_Exception
  54. */
  55. public function __construct(Iterator $iterator)
  56. {
  57. if (!$iterator instanceof Countable) {
  58. /**
  59. * @see Zend_Paginator_Exception
  60. */
  61. require_once 'Zend/Paginator/Exception.php';
  62. throw new Zend_Paginator_Exception('Iterator must implement Countable');
  63. }
  64. $this->_iterator = $iterator;
  65. $this->_count = count($iterator);
  66. }
  67. /**
  68. * Returns an iterator of items for a page, or an empty array.
  69. *
  70. * @param integer $offset Page offset
  71. * @param integer $itemCountPerPage Number of items per page
  72. * @return LimitIterator|array
  73. */
  74. public function getItems($offset, $itemCountPerPage)
  75. {
  76. if ($this->_count == 0) {
  77. return array();
  78. }
  79. // @link http://bugs.php.net/bug.php?id=49906 | ZF-8084
  80. // return new LimitIterator($this->_iterator, $offset, $itemCountPerPage);
  81. return new Zend_Paginator_SerializableLimitIterator($this->_iterator, $offset, $itemCountPerPage);
  82. }
  83. /**
  84. * Returns the total number of rows in the collection.
  85. *
  86. * @return integer
  87. */
  88. public function count()
  89. {
  90. return $this->_count;
  91. }
  92. }