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