ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RowIterator.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Iterator;
7 
8 class RowIterator implements Iterator
9 {
15  private $subject;
16 
22  private $position = 1;
23 
29  private $startRow = 1;
30 
36  private $endRow = 1;
37 
45  public function __construct(Worksheet $subject, $startRow = 1, $endRow = null)
46  {
47  // Set subject
48  $this->subject = $subject;
49  $this->resetEnd($endRow);
50  $this->resetStart($startRow);
51  }
52 
60  public function resetStart(int $startRow = 1)
61  {
62  if ($startRow > $this->subject->getHighestRow()) {
63  throw new PhpSpreadsheetException(
64  "Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})"
65  );
66  }
67 
68  $this->startRow = $startRow;
69  if ($this->endRow < $this->startRow) {
70  $this->endRow = $this->startRow;
71  }
72  $this->seek($startRow);
73 
74  return $this;
75  }
76 
84  public function resetEnd($endRow = null)
85  {
86  $this->endRow = $endRow ?: $this->subject->getHighestRow();
87 
88  return $this;
89  }
90 
98  public function seek(int $row = 1)
99  {
100  if (($row < $this->startRow) || ($row > $this->endRow)) {
101  throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
102  }
103  $this->position = $row;
104 
105  return $this;
106  }
107 
111  public function rewind(): void
112  {
113  $this->position = $this->startRow;
114  }
115 
121  public function current()
122  {
123  return new Row($this->subject, $this->position);
124  }
125 
129  public function key(): int
130  {
131  return $this->position;
132  }
133 
137  public function next(): void
138  {
139  ++$this->position;
140  }
141 
145  public function prev(): void
146  {
147  --$this->position;
148  }
149 
153  public function valid(): bool
154  {
155  return $this->position <= $this->endRow && $this->position >= $this->startRow;
156  }
157 }
resetEnd($endRow=null)
(Re)Set the end row.
Definition: RowIterator.php:84
valid()
Indicate if more rows exist in the worksheet range of rows that we&#39;re iterating.
rewind()
Rewind the iterator to the starting row.
next()
Set the iterator to its next value.
key()
Return the current iterator key.
resetStart(int $startRow=1)
(Re)Set the start row and the current row pointer.
Definition: RowIterator.php:60
__construct(Worksheet $subject, $startRow=1, $endRow=null)
Create a new row iterator.
Definition: RowIterator.php:45
prev()
Set the iterator to its previous value.
seek(int $row=1)
Set the row pointer to the selected row.
Definition: RowIterator.php:98
current()
Return the current row in this worksheet.
$row