RowIterator.php 3.5 KB

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