RowIterator.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * PHPExcel_Worksheet_RowIterator
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Worksheet
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Worksheet_RowIterator implements Iterator
  28. {
  29. /**
  30. * PHPExcel_Worksheet to iterate
  31. *
  32. * @var PHPExcel_Worksheet
  33. */
  34. private $subject;
  35. /**
  36. * Current iterator position
  37. *
  38. * @var int
  39. */
  40. private $position = 1;
  41. /**
  42. * Start position
  43. *
  44. * @var int
  45. */
  46. private $startRow = 1;
  47. /**
  48. * End position
  49. *
  50. * @var int
  51. */
  52. private $endRow = 1;
  53. /**
  54. * Create a new row iterator
  55. *
  56. * @param PHPExcel_Worksheet $subject The worksheet to iterate over
  57. * @param integer $startRow The row number at which to start iterating
  58. * @param integer $endRow Optionally, the row number at which to stop iterating
  59. */
  60. public function __construct(PHPExcel_Worksheet $subject, $startRow = 1, $endRow = null)
  61. {
  62. // Set subject
  63. $this->subject = $subject;
  64. $this->resetEnd($endRow);
  65. $this->resetStart($startRow);
  66. }
  67. /**
  68. * Destructor
  69. */
  70. public function __destruct()
  71. {
  72. unset($this->subject);
  73. }
  74. /**
  75. * (Re)Set the start row and the current row pointer
  76. *
  77. * @param integer $startRow The row number at which to start iterating
  78. * @return PHPExcel_Worksheet_RowIterator
  79. * @throws PHPExcel_Exception
  80. */
  81. public function resetStart($startRow = 1)
  82. {
  83. if ($startRow > $this->subject->getHighestRow()) {
  84. throw new PHPExcel_Exception("Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})");
  85. }
  86. $this->startRow = $startRow;
  87. if ($this->endRow < $this->startRow) {
  88. $this->endRow = $this->startRow;
  89. }
  90. $this->seek($startRow);
  91. return $this;
  92. }
  93. /**
  94. * (Re)Set the end row
  95. *
  96. * @param integer $endRow The row number at which to stop iterating
  97. * @return PHPExcel_Worksheet_RowIterator
  98. */
  99. public function resetEnd($endRow = null)
  100. {
  101. $this->endRow = ($endRow) ? $endRow : $this->subject->getHighestRow();
  102. return $this;
  103. }
  104. /**
  105. * Set the row pointer to the selected row
  106. *
  107. * @param integer $row The row number to set the current pointer at
  108. * @return PHPExcel_Worksheet_RowIterator
  109. * @throws PHPExcel_Exception
  110. */
  111. public function seek($row = 1)
  112. {
  113. if (($row < $this->startRow) || ($row > $this->endRow)) {
  114. throw new PHPExcel_Exception("Row $row is out of range ({$this->startRow} - {$this->endRow})");
  115. }
  116. $this->position = $row;
  117. return $this;
  118. }
  119. /**
  120. * Rewind the iterator to the starting row
  121. */
  122. public function rewind()
  123. {
  124. $this->position = $this->startRow;
  125. }
  126. /**
  127. * Return the current row in this worksheet
  128. *
  129. * @return PHPExcel_Worksheet_Row
  130. */
  131. public function current()
  132. {
  133. return new PHPExcel_Worksheet_Row($this->subject, $this->position);
  134. }
  135. /**
  136. * Return the current iterator key
  137. *
  138. * @return int
  139. */
  140. public function key()
  141. {
  142. return $this->position;
  143. }
  144. /**
  145. * Set the iterator to its next value
  146. */
  147. public function next()
  148. {
  149. ++$this->position;
  150. }
  151. /**
  152. * Set the iterator to its previous value
  153. */
  154. public function prev()
  155. {
  156. if ($this->position <= $this->startRow) {
  157. throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->startRow} - {$this->endRow})");
  158. }
  159. --$this->position;
  160. }
  161. /**
  162. * Indicate if more rows exist in the worksheet range of rows that we're iterating
  163. *
  164. * @return boolean
  165. */
  166. public function valid()
  167. {
  168. return $this->position <= $this->endRow;
  169. }
  170. }