1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
use Iterator; |
|
|
|
|
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
7
|
|
|
|
8
|
|
|
class RowIterator implements Iterator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Worksheet to iterate. |
12
|
|
|
* |
13
|
|
|
* @var Worksheet |
14
|
|
|
*/ |
15
|
|
|
private $subject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Current iterator position. |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private $position = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Start position. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $startRow = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* End position. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $endRow = 1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new row iterator. |
40
|
|
|
* |
41
|
|
|
* @param Worksheet $subject The worksheet to iterate over |
42
|
|
|
* @param int $startRow The row number at which to start iterating |
43
|
|
|
* @param int $endRow Optionally, the row number at which to stop iterating |
44
|
|
|
*/ |
45
|
20 |
|
public function __construct(Worksheet $subject, $startRow = 1, $endRow = null) |
46
|
|
|
{ |
47
|
|
|
// Set subject |
48
|
20 |
|
$this->subject = $subject; |
49
|
20 |
|
$this->resetEnd($endRow); |
50
|
20 |
|
$this->resetStart($startRow); |
51
|
20 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* (Re)Set the start row and the current row pointer. |
55
|
|
|
* |
56
|
|
|
* @param int $startRow The row number at which to start iterating |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
20 |
|
public function resetStart(int $startRow = 1) |
61
|
|
|
{ |
62
|
20 |
|
if ($startRow > $this->subject->getHighestRow()) { |
63
|
1 |
|
throw new PhpSpreadsheetException( |
64
|
1 |
|
"Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})" |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
20 |
|
$this->startRow = $startRow; |
69
|
20 |
|
if ($this->endRow < $this->startRow) { |
70
|
1 |
|
$this->endRow = $this->startRow; |
71
|
|
|
} |
72
|
20 |
|
$this->seek($startRow); |
73
|
|
|
|
74
|
20 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* (Re)Set the end row. |
79
|
|
|
* |
80
|
|
|
* @param int $endRow The row number at which to stop iterating |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
20 |
|
public function resetEnd($endRow = null) |
85
|
|
|
{ |
86
|
20 |
|
$this->endRow = $endRow ?: $this->subject->getHighestRow(); |
87
|
|
|
|
88
|
20 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the row pointer to the selected row. |
93
|
|
|
* |
94
|
|
|
* @param int $row The row number to set the current pointer at |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
20 |
|
public function seek(int $row = 1) |
99
|
|
|
{ |
100
|
20 |
|
if (($row < $this->startRow) || ($row > $this->endRow)) { |
101
|
1 |
|
throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})"); |
102
|
|
|
} |
103
|
20 |
|
$this->position = $row; |
104
|
|
|
|
105
|
20 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Rewind the iterator to the starting row. |
110
|
|
|
*/ |
111
|
6 |
|
public function rewind(): void |
112
|
|
|
{ |
113
|
6 |
|
$this->position = $this->startRow; |
114
|
6 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the current row in this worksheet. |
118
|
|
|
* |
119
|
|
|
* @return Row |
120
|
|
|
*/ |
121
|
15 |
|
public function current() |
122
|
|
|
{ |
123
|
15 |
|
return new Row($this->subject, $this->position); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the current iterator key. |
128
|
|
|
*/ |
129
|
4 |
|
public function key(): int |
130
|
|
|
{ |
131
|
4 |
|
return $this->position; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set the iterator to its next value. |
136
|
|
|
*/ |
137
|
16 |
|
public function next(): void |
138
|
|
|
{ |
139
|
16 |
|
++$this->position; |
140
|
16 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the iterator to its previous value. |
144
|
|
|
*/ |
145
|
2 |
|
public function prev(): void |
146
|
|
|
{ |
147
|
2 |
|
--$this->position; |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Indicate if more rows exist in the worksheet range of rows that we're iterating. |
152
|
|
|
*/ |
153
|
17 |
|
public function valid(): bool |
154
|
|
|
{ |
155
|
17 |
|
return $this->position <= $this->endRow && $this->position >= $this->startRow; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: