ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ColumnCellIterator.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
10 {
16  private $currentRow;
17 
23  private $columnIndex;
24 
30  private $startRow = 1;
31 
37  private $endRow = 1;
38 
47  public function __construct(?Worksheet $subject = null, $columnIndex = 'A', $startRow = 1, $endRow = null)
48  {
49  // Set subject
50  $this->worksheet = $subject;
51  $this->columnIndex = Coordinate::columnIndexFromString($columnIndex);
52  $this->resetEnd($endRow);
53  $this->resetStart($startRow);
54  }
55 
63  public function resetStart(int $startRow = 1)
64  {
65  $this->startRow = $startRow;
67  $this->seek($startRow);
68 
69  return $this;
70  }
71 
79  public function resetEnd($endRow = null)
80  {
81  $this->endRow = $endRow ?: $this->worksheet->getHighestRow();
83 
84  return $this;
85  }
86 
94  public function seek(int $row = 1)
95  {
96  if ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $row))) {
97  throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
98  }
99  if (($row < $this->startRow) || ($row > $this->endRow)) {
100  throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
101  }
102  $this->currentRow = $row;
103 
104  return $this;
105  }
106 
110  public function rewind(): void
111  {
112  $this->currentRow = $this->startRow;
113  }
114 
118  public function current(): ?Cell
119  {
120  return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow);
121  }
122 
126  public function key(): int
127  {
128  return $this->currentRow;
129  }
130 
134  public function next(): void
135  {
136  do {
138  } while (
139  ($this->onlyExistingCells) &&
140  (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) &&
141  ($this->currentRow <= $this->endRow)
142  );
143  }
144 
148  public function prev(): void
149  {
150  do {
152  } while (
153  ($this->onlyExistingCells) &&
154  (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) &&
155  ($this->currentRow >= $this->startRow)
156  );
157  }
158 
162  public function valid(): bool
163  {
164  return $this->currentRow <= $this->endRow && $this->currentRow >= $this->startRow;
165  }
166 
170  protected function adjustForExistingOnlyRange(): void
171  {
172  if ($this->onlyExistingCells) {
173  while (
174  (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->startRow)) &&
175  ($this->startRow <= $this->endRow)
176  ) {
177  ++$this->startRow;
178  }
179  while (
180  (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->endRow)) &&
181  ($this->endRow >= $this->startRow)
182  ) {
183  --$this->endRow;
184  }
185  }
186  }
187 }
resetStart(int $startRow=1)
(Re)Set the start row and the current row pointer.
adjustForExistingOnlyRange()
Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
prev()
Set the iterator to its previous value.
__construct(?Worksheet $subject=null, $columnIndex='A', $startRow=1, $endRow=null)
Create a new row iterator.
seek(int $row=1)
Set the row pointer to the selected row.
$row
rewind()
Rewind the iterator to the starting row.
current()
Return the current cell in this worksheet column.
static columnIndexFromString($pString)
Column index from string.
Definition: Coordinate.php:265
valid()
Indicate if more rows exist in the worksheet range of rows that we&#39;re iterating.