ColumnIterator.php 4.6 KB

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