For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.8 KiB

  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. }