Lookbehind.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Iterator;
  36. /**
  37. * Class \Hoa\Iterator\Lookbehind.
  38. *
  39. * Look behind iterator.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. class Lookbehind extends IteratorIterator implements Outer
  45. {
  46. /**
  47. * Current iterator.
  48. *
  49. * @var \Iterator
  50. */
  51. protected $_iterator = null;
  52. /**
  53. * Previous key.
  54. *
  55. * @var mixed
  56. */
  57. protected $_previousKey = -1;
  58. /**
  59. * Previous value.
  60. *
  61. * @var mixed
  62. */
  63. protected $_previousCurrent = null;
  64. /**
  65. * Construct.
  66. *
  67. * @param \Iterator $iterator Iterator.
  68. */
  69. public function __construct(\Iterator $iterator)
  70. {
  71. $this->_iterator = $iterator;
  72. return;
  73. }
  74. /**
  75. * Get inner iterator.
  76. *
  77. * @return \Iterator
  78. */
  79. public function getInnerIterator()
  80. {
  81. return $this->_iterator;
  82. }
  83. /**
  84. * Return the current element.
  85. *
  86. * @return mixed
  87. */
  88. public function current()
  89. {
  90. return $this->getInnerIterator()->current();
  91. }
  92. /**
  93. * Return the key of the current element.
  94. *
  95. * @return mixed
  96. */
  97. public function key()
  98. {
  99. return $this->getInnerIterator()->key();
  100. }
  101. /**
  102. * Move forward to next element.
  103. *
  104. * @return void
  105. */
  106. public function next()
  107. {
  108. $this->_previousKey = $this->key();
  109. $this->_previousCurrent = $this->current();
  110. return $this->getInnerIterator()->next();
  111. }
  112. /**
  113. * Rewind the iterator to the first element.
  114. *
  115. * @return void
  116. */
  117. public function rewind()
  118. {
  119. $this->_previousKey = -1;
  120. $this->_previousCurrent = null;
  121. return $this->getInnerIterator()->rewind();
  122. }
  123. /**
  124. * Check if current position is valid.
  125. *
  126. * @return bool
  127. */
  128. public function valid()
  129. {
  130. return $this->getInnerIterator()->valid();
  131. }
  132. /**
  133. * Check whether there is a previous element.
  134. *
  135. * @return bool
  136. */
  137. public function hasPrevious()
  138. {
  139. return -1 !== $this->_previousKey;
  140. }
  141. /**
  142. * Get previous value.
  143. *
  144. * @return mixed
  145. */
  146. public function getPrevious()
  147. {
  148. return $this->_previousCurrent;
  149. }
  150. /**
  151. * Get previous key.
  152. *
  153. * @return mixed
  154. */
  155. public function getPreviousKey()
  156. {
  157. return $this->_previousKey;
  158. }
  159. }