SerializableLimitIterator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @category Zend
  23. * @package Zend_Paginator
  24. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Paginator_SerializableLimitIterator extends LimitIterator implements Serializable, ArrayAccess
  28. {
  29. /**
  30. * Offset to first element
  31. *
  32. * @var int
  33. */
  34. private $_offset;
  35. /**
  36. * Maximum number of elements to show or -1 for all
  37. *
  38. * @var int
  39. */
  40. private $_count;
  41. /**
  42. * Construct a Zend_Paginator_SerializableLimitIterator
  43. *
  44. * @param Iterator $it Iterator to limit (must be serializable by un-/serialize)
  45. * @param int $offset Offset to first element
  46. * @param int $count Maximum number of elements to show or -1 for all
  47. * @see LimitIterator::__construct
  48. */
  49. public function __construct (Iterator $it, $offset=0, $count=-1)
  50. {
  51. parent::__construct($it, $offset, $count);
  52. $this->_offset = $offset;
  53. $this->_count = $count;
  54. }
  55. /**
  56. * @return string representation of the instance
  57. */
  58. public function serialize()
  59. {
  60. return serialize(array(
  61. 'it' => $this->getInnerIterator(),
  62. 'offset' => $this->_offset,
  63. 'count' => $this->_count,
  64. 'pos' => $this->getPosition(),
  65. ));
  66. }
  67. /**
  68. * @param string $data representation of the instance
  69. */
  70. public function unserialize($data)
  71. {
  72. $dataArr = unserialize($data);
  73. $this->__construct($dataArr['it'], $dataArr['offset'], $dataArr['count']);
  74. $this->seek($dataArr['pos']+$dataArr['offset']);
  75. }
  76. /**
  77. * Returns value of the Iterator
  78. *
  79. * @param int $offset
  80. * @return mixed
  81. */
  82. public function offsetGet($offset)
  83. {
  84. $currentOffset = $this->key();
  85. $this->seek($offset);
  86. $current = $this->current();
  87. $this->seek($currentOffset);
  88. return $current;
  89. }
  90. /**
  91. * Does nothing
  92. * Required by the ArrayAccess implementation
  93. *
  94. * @param int $offset
  95. * @param mixed $value
  96. */
  97. public function offsetSet($offset, $value)
  98. {
  99. }
  100. /**
  101. * Determine if a value of Iterator is set and is not NULL
  102. *
  103. * @param int $offset
  104. */
  105. public function offsetExists($offset)
  106. {
  107. if ($offset > 0 && $offset < $this->_count) {
  108. try {
  109. $currentOffset = $this->key();
  110. $this->seek($offset);
  111. $current = $this->current();
  112. $this->seek($currentOffset);
  113. return null !== $current;
  114. } catch (OutOfBoundsException $e) {
  115. // reset position in case of exception is assigned null
  116. $this->rewind();
  117. $this->seek($currentOffset);
  118. return false;
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Does nothing
  125. * Required by the ArrayAccess implementation
  126. *
  127. * @param int $offset
  128. */
  129. public function offsetUnset($offset)
  130. {
  131. }
  132. }