RowIterator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Worksheet_RowIterator implements Iterator
  8. {
  9. /**
  10. * PHPExcel_Worksheet to iterate
  11. *
  12. * @var PHPExcel_Worksheet
  13. */
  14. private $subject;
  15. /**
  16. * Current iterator position
  17. *
  18. * @var int
  19. */
  20. private $position = 1;
  21. /**
  22. * Start position
  23. *
  24. * @var int
  25. */
  26. private $startRow = 1;
  27. /**
  28. * End position
  29. *
  30. * @var int
  31. */
  32. private $endRow = 1;
  33. /**
  34. * Create a new row iterator
  35. *
  36. * @param PHPExcel_Worksheet $subject The worksheet to iterate over
  37. * @param integer $startRow The row number at which to start iterating
  38. * @param integer $endRow Optionally, the row number at which to stop iterating
  39. */
  40. public function __construct(PHPExcel_Worksheet $subject, $startRow = 1, $endRow = null)
  41. {
  42. // Set subject
  43. $this->subject = $subject;
  44. $this->resetEnd($endRow);
  45. $this->resetStart($startRow);
  46. }
  47. /**
  48. * Destructor
  49. */
  50. public function __destruct()
  51. {
  52. unset($this->subject);
  53. }
  54. /**
  55. * (Re)Set the start row and the current row pointer
  56. *
  57. * @param integer $startRow The row number at which to start iterating
  58. * @return PHPExcel_Worksheet_RowIterator
  59. * @throws PHPExcel_Exception
  60. */
  61. public function resetStart($startRow = 1)
  62. {
  63. if ($startRow > $this->subject->getHighestRow()) {
  64. throw new PHPExcel_Exception("Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})");
  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 integer $endRow The row number at which to stop iterating
  77. * @return PHPExcel_Worksheet_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 integer $row The row number to set the current pointer at
  88. * @return PHPExcel_Worksheet_RowIterator
  89. * @throws PHPExcel_Exception
  90. */
  91. public function seek($row = 1)
  92. {
  93. if (($row < $this->startRow) || ($row > $this->endRow)) {
  94. throw new PHPExcel_Exception("Row $row is out of range ({$this->startRow} - {$this->endRow})");
  95. }
  96. $this->position = $row;
  97. return $this;
  98. }
  99. /**
  100. * Rewind the iterator to the starting row
  101. */
  102. public function rewind()
  103. {
  104. $this->position = $this->startRow;
  105. }
  106. /**
  107. * Return the current row in this worksheet
  108. *
  109. * @return PHPExcel_Worksheet_Row
  110. */
  111. public function current()
  112. {
  113. return new PHPExcel_Worksheet_Row($this->subject, $this->position);
  114. }
  115. /**
  116. * Return the current iterator key
  117. *
  118. * @return int
  119. */
  120. public function key()
  121. {
  122. return $this->position;
  123. }
  124. /**
  125. * Set the iterator to its next value
  126. */
  127. public function next()
  128. {
  129. ++$this->position;
  130. }
  131. /**
  132. * Set the iterator to its previous value
  133. */
  134. public function prev()
  135. {
  136. if ($this->position <= $this->startRow) {
  137. throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->startRow} - {$this->endRow})");
  138. }
  139. --$this->position;
  140. }
  141. /**
  142. * Indicate if more rows exist in the worksheet range of rows that we're iterating
  143. *
  144. * @return boolean
  145. */
  146. public function valid()
  147. {
  148. return $this->position <= $this->endRow;
  149. }
  150. }