ColumnIterator.php 4.4 KB

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