ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ColumnIterator.php
Go to the documentation of this file.
1<?php
2
4
5use Iterator;
8use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
9
10class ColumnIterator implements Iterator
11{
17 private $worksheet;
18
25
31 private $startColumnIndex = 1;
32
38 private $endColumnIndex = 1;
39
47 public function __construct(Worksheet $worksheet, $startColumn = 'A', $endColumn = null)
48 {
49 // Set subject
50 $this->worksheet = $worksheet;
51 $this->resetEnd($endColumn);
52 $this->resetStart($startColumn);
53 }
54
58 public function __destruct()
59 {
60 // @phpstan-ignore-next-line
61 $this->worksheet = null;
62 }
63
71 public function resetStart(string $startColumn = 'A')
72 {
74 if ($startColumnIndex > Coordinate::columnIndexFromString($this->worksheet->getHighestColumn())) {
75 throw new Exception(
76 "Start column ({$startColumn}) is beyond highest column ({$this->worksheet->getHighestColumn()})"
77 );
78 }
79
80 $this->startColumnIndex = $startColumnIndex;
81 if ($this->endColumnIndex < $this->startColumnIndex) {
82 $this->endColumnIndex = $this->startColumnIndex;
83 }
84 $this->seek($startColumn);
85
86 return $this;
87 }
88
96 public function resetEnd($endColumn = null)
97 {
98 $endColumn = $endColumn ?: $this->worksheet->getHighestColumn();
99 $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);
100
101 return $this;
102 }
103
111 public function seek(string $column = 'A')
112 {
113 $column = Coordinate::columnIndexFromString($column);
114 if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) {
115 throw new PhpSpreadsheetException(
116 "Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})"
117 );
118 }
119 $this->currentColumnIndex = $column;
120
121 return $this;
122 }
123
127 public function rewind(): void
128 {
129 $this->currentColumnIndex = $this->startColumnIndex;
130 }
131
137 public function current()
138 {
139 return new Column($this->worksheet, Coordinate::stringFromColumnIndex($this->currentColumnIndex));
140 }
141
145 public function key(): string
146 {
147 return Coordinate::stringFromColumnIndex($this->currentColumnIndex);
148 }
149
153 public function next(): void
154 {
156 }
157
161 public function prev(): void
162 {
164 }
165
169 public function valid(): bool
170 {
171 return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex;
172 }
173}
An exception for terminatinating execution or to throw for unit testing.
Helper class to manipulate cell coordinates.
Definition: Coordinate.php:15
static columnIndexFromString($pString)
Column index from string.
Definition: Coordinate.php:265
static stringFromColumnIndex($columnIndex)
String from column index.
Definition: Coordinate.php:313
seek(string $column='A')
Set the column pointer to the selected column.
prev()
Set the iterator to its previous value.
next()
Set the iterator to its next value.
valid()
Indicate if more columns exist in the worksheet range of columns that we're iterating.
current()
Return the current column in this worksheet.
resetEnd($endColumn=null)
(Re)Set the end column.
__construct(Worksheet $worksheet, $startColumn='A', $endColumn=null)
Create a new column iterator.
resetStart(string $startColumn='A')
(Re)Set the start column and the current column pointer.
rewind()
Rewind the iterator to the starting column.