0fdb6e4a65f12ddd3893a3b14a5d429f50af40c0.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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. /**
  28. * PHPExcel_Worksheet_RowIterator
  29. *
  30. * Used to iterate rows in a PHPExcel_Worksheet
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Worksheet
  34. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Worksheet_RowIterator implements Iterator
  37. {
  38. /**
  39. * PHPExcel_Worksheet to iterate
  40. *
  41. * @var PHPExcel_Worksheet
  42. */
  43. private $_subject;
  44. /**
  45. * Current iterator position
  46. *
  47. * @var int
  48. */
  49. private $_position = 1;
  50. /**
  51. * Start position
  52. *
  53. * @var int
  54. */
  55. private $_startRow = 1;
  56. /**
  57. * End position
  58. *
  59. * @var int
  60. */
  61. private $_endRow = 1;
  62. /**
  63. * Create a new row iterator
  64. *
  65. * @param PHPExcel_Worksheet $subject The worksheet to iterate over
  66. * @param integer $startRow The row number at which to start iterating
  67. * @param integer $endRow Optionally, the row number at which to stop iterating
  68. */
  69. public function __construct(PHPExcel_Worksheet $subject = null, $startRow = 1, $endRow = null) {
  70. // Set subject
  71. $this->_subject = $subject;
  72. $this->resetEnd($endRow);
  73. $this->resetStart($startRow);
  74. }
  75. /**
  76. * Destructor
  77. */
  78. public function __destruct() {
  79. unset($this->_subject);
  80. }
  81. /**
  82. * (Re)Set the start row and the current row pointer
  83. *
  84. * @param integer $startRow The row number at which to start iterating
  85. * @return PHPExcel_Worksheet_RowIterator
  86. */
  87. public function resetStart($startRow = 1) {
  88. $this->_startRow = $startRow;
  89. $this->seek($startRow);
  90. return $this;
  91. }
  92. /**
  93. * (Re)Set the end row
  94. *
  95. * @param integer $endRow The row number at which to stop iterating
  96. * @return PHPExcel_Worksheet_RowIterator
  97. */
  98. public function resetEnd($endRow = null) {
  99. $this->_endRow = ($endRow) ? $endRow : $this->_subject->getHighestRow();
  100. return $this;
  101. }
  102. /**
  103. * Set the row pointer to the selected row
  104. *
  105. * @param integer $row The row number to set the current pointer at
  106. * @return PHPExcel_Worksheet_RowIterator
  107. * @throws PHPExcel_Exception
  108. */
  109. public function seek($row = 1) {
  110. if (($row < $this->_startRow) || ($row > $this->_endRow)) {
  111. throw new PHPExcel_Exception("Row $row is out of range ({$this->_startRow} - {$this->_endRow})");
  112. }
  113. $this->_position = $row;
  114. return $this;
  115. }
  116. /**
  117. * Rewind the iterator to the starting row
  118. */
  119. public function rewind() {
  120. $this->_position = $this->_startRow;
  121. }
  122. /**
  123. * Return the current row in this worksheet
  124. *
  125. * @return PHPExcel_Worksheet_Row
  126. */
  127. public function current() {
  128. return new PHPExcel_Worksheet_Row($this->_subject, $this->_position);
  129. }
  130. /**
  131. * Return the current iterator key
  132. *
  133. * @return int
  134. */
  135. public function key() {
  136. return $this->_position;
  137. }
  138. /**
  139. * Set the iterator to its next value
  140. */
  141. public function next() {
  142. ++$this->_position;
  143. }
  144. /**
  145. * Set the iterator to its previous value
  146. */
  147. public function prev() {
  148. if ($this->_position <= $this->_startRow) {
  149. throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->_startRow} - {$this->_endRow})");
  150. }
  151. --$this->_position;
  152. }
  153. /**
  154. * Indicate if more rows exist in the worksheet range of rows that we're iterating
  155. *
  156. * @return boolean
  157. */
  158. public function valid() {
  159. return $this->_position <= $this->_endRow;
  160. }
  161. }