RowIterator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. class RowIterator implements \Iterator
  5. {
  6. /**
  7. * Worksheet to iterate.
  8. *
  9. * @var Worksheet
  10. */
  11. private $subject;
  12. /**
  13. * Current iterator position.
  14. *
  15. * @var int
  16. */
  17. private $position = 1;
  18. /**
  19. * Start position.
  20. *
  21. * @var int
  22. */
  23. private $startRow = 1;
  24. /**
  25. * End position.
  26. *
  27. * @var int
  28. */
  29. private $endRow = 1;
  30. /**
  31. * Create a new row iterator.
  32. *
  33. * @param Worksheet $subject The worksheet to iterate over
  34. * @param int $startRow The row number at which to start iterating
  35. * @param int $endRow Optionally, the row number at which to stop iterating
  36. */
  37. public function __construct(Worksheet $subject, $startRow = 1, $endRow = null)
  38. {
  39. // Set subject
  40. $this->subject = $subject;
  41. $this->resetEnd($endRow);
  42. $this->resetStart($startRow);
  43. }
  44. /**
  45. * Destructor.
  46. */
  47. public function __destruct()
  48. {
  49. unset($this->subject);
  50. }
  51. /**
  52. * (Re)Set the start row and the current row pointer.
  53. *
  54. * @param int $startRow The row number at which to start iterating
  55. *
  56. * @throws PhpSpreadsheetException
  57. *
  58. * @return RowIterator
  59. */
  60. public function resetStart($startRow = 1)
  61. {
  62. if ($startRow > $this->subject->getHighestRow()) {
  63. throw new PhpSpreadsheetException("Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})");
  64. }
  65. $this->startRow = $startRow;
  66. if ($this->endRow < $this->startRow) {
  67. $this->endRow = $this->startRow;
  68. }
  69. $this->seek($startRow);
  70. return $this;
  71. }
  72. /**
  73. * (Re)Set the end row.
  74. *
  75. * @param int $endRow The row number at which to stop iterating
  76. *
  77. * @return RowIterator
  78. */
  79. public function resetEnd($endRow = null)
  80. {
  81. $this->endRow = ($endRow) ? $endRow : $this->subject->getHighestRow();
  82. return $this;
  83. }
  84. /**
  85. * Set the row pointer to the selected row.
  86. *
  87. * @param int $row The row number to set the current pointer at
  88. *
  89. * @throws PhpSpreadsheetException
  90. *
  91. * @return RowIterator
  92. */
  93. public function seek($row = 1)
  94. {
  95. if (($row < $this->startRow) || ($row > $this->endRow)) {
  96. throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
  97. }
  98. $this->position = $row;
  99. return $this;
  100. }
  101. /**
  102. * Rewind the iterator to the starting row.
  103. */
  104. public function rewind()
  105. {
  106. $this->position = $this->startRow;
  107. }
  108. /**
  109. * Return the current row in this worksheet.
  110. *
  111. * @return Row
  112. */
  113. public function current()
  114. {
  115. return new Row($this->subject, $this->position);
  116. }
  117. /**
  118. * Return the current iterator key.
  119. *
  120. * @return int
  121. */
  122. public function key()
  123. {
  124. return $this->position;
  125. }
  126. /**
  127. * Set the iterator to its next value.
  128. */
  129. public function next()
  130. {
  131. ++$this->position;
  132. }
  133. /**
  134. * Set the iterator to its previous value.
  135. */
  136. public function prev()
  137. {
  138. --$this->position;
  139. }
  140. /**
  141. * Indicate if more rows exist in the worksheet range of rows that we're iterating.
  142. *
  143. * @return bool
  144. */
  145. public function valid()
  146. {
  147. return $this->position <= $this->endRow && $this->position >= $this->startRow;
  148. }
  149. }