1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU Lesser General Public |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16
|
|
|
* Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Lesser General Public |
19
|
|
|
* License along with this library; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21
|
|
|
* |
22
|
|
|
* @category PhpSpreadsheet |
23
|
|
|
* |
24
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
25
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
26
|
|
|
*/ |
27
|
|
|
class RowCellIterator extends CellIterator implements \Iterator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Row index. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $rowIndex; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Start position. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $startColumn = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* End position. |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $endColumn = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new column iterator. |
52
|
|
|
* |
53
|
|
|
* @param \PhpOffice\PhpSpreadsheet\Worksheet $subject The worksheet to iterate over |
54
|
|
|
* @param int $rowIndex The row that we want to iterate |
55
|
|
|
* @param string $startColumn The column address at which to start iterating |
56
|
|
|
* @param string $endColumn Optionally, the column address at which to stop iterating |
57
|
|
|
*/ |
58
|
9 |
View Code Duplication |
public function __construct(\PhpOffice\PhpSpreadsheet\Worksheet $subject = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
// Set subject and row index |
61
|
9 |
|
$this->subject = $subject; |
62
|
9 |
|
$this->rowIndex = $rowIndex; |
63
|
9 |
|
$this->resetEnd($endColumn); |
64
|
9 |
|
$this->resetStart($startColumn); |
65
|
9 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Destructor. |
69
|
|
|
*/ |
70
|
9 |
|
public function __destruct() |
71
|
|
|
{ |
72
|
9 |
|
unset($this->subject); |
73
|
9 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* (Re)Set the start column and the current column pointer. |
77
|
|
|
* |
78
|
|
|
* @param int $startColumn The column address at which to start iterating |
79
|
|
|
* |
80
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
81
|
|
|
* |
82
|
|
|
* @return RowCellIterator |
83
|
|
|
*/ |
84
|
9 |
|
public function resetStart($startColumn = 'A') |
85
|
|
|
{ |
86
|
9 |
|
$startColumnIndex = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($startColumn) - 1; |
87
|
9 |
|
$this->startColumn = $startColumnIndex; |
88
|
9 |
|
$this->adjustForExistingOnlyRange(); |
89
|
9 |
|
$this->seek(\PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($this->startColumn)); |
90
|
|
|
|
91
|
9 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* (Re)Set the end column. |
96
|
|
|
* |
97
|
|
|
* @param string $endColumn The column address at which to stop iterating |
98
|
|
|
* |
99
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
100
|
|
|
* |
101
|
|
|
* @return RowCellIterator |
102
|
|
|
*/ |
103
|
9 |
View Code Duplication |
public function resetEnd($endColumn = null) |
|
|
|
|
104
|
|
|
{ |
105
|
9 |
|
$endColumn = ($endColumn) ? $endColumn : $this->subject->getHighestColumn(); |
106
|
9 |
|
$this->endColumn = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($endColumn) - 1; |
107
|
9 |
|
$this->adjustForExistingOnlyRange(); |
108
|
|
|
|
109
|
9 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set the column pointer to the selected column. |
114
|
|
|
* |
115
|
|
|
* @param string $column The column address to set the current pointer at |
116
|
|
|
* |
117
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
118
|
|
|
* |
119
|
|
|
* @return RowCellIterator |
120
|
|
|
*/ |
121
|
9 |
View Code Duplication |
public function seek($column = 'A') |
|
|
|
|
122
|
|
|
{ |
123
|
9 |
|
$column = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($column) - 1; |
124
|
9 |
|
if (($column < $this->startColumn) || ($column > $this->endColumn)) { |
125
|
|
|
throw new \PhpOffice\PhpSpreadsheet\Exception("Column $column is out of range ({$this->startColumn} - {$this->endColumn})"); |
126
|
9 |
|
} elseif ($this->onlyExistingCells && !($this->subject->cellExistsByColumnAndRow($column, $this->rowIndex))) { |
127
|
|
|
throw new \PhpOffice\PhpSpreadsheet\Exception('In "IterateOnlyExistingCells" mode and Cell does not exist'); |
128
|
|
|
} |
129
|
9 |
|
$this->position = $column; |
130
|
|
|
|
131
|
9 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Rewind the iterator to the starting column. |
136
|
|
|
*/ |
137
|
3 |
|
public function rewind() |
138
|
|
|
{ |
139
|
3 |
|
$this->position = $this->startColumn; |
140
|
3 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Return the current cell in this worksheet row. |
144
|
|
|
* |
145
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Cell |
146
|
|
|
*/ |
147
|
5 |
|
public function current() |
148
|
|
|
{ |
149
|
5 |
|
return $this->subject->getCellByColumnAndRow($this->position, $this->rowIndex); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return the current iterator key. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
3 |
|
public function key() |
158
|
|
|
{ |
159
|
3 |
|
return \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($this->position); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the iterator to its next value. |
164
|
|
|
*/ |
165
|
5 |
View Code Duplication |
public function next() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
do { |
168
|
5 |
|
++$this->position; |
169
|
5 |
|
} while (($this->onlyExistingCells) && |
170
|
|
|
(!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) && |
171
|
|
|
($this->position <= $this->endColumn)); |
172
|
5 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the iterator to its previous value. |
176
|
|
|
* |
177
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
178
|
|
|
*/ |
179
|
2 |
|
public function prev() |
180
|
|
|
{ |
181
|
2 |
View Code Duplication |
if ($this->position <= $this->startColumn) { |
|
|
|
|
182
|
1 |
|
throw new \PhpOffice\PhpSpreadsheet\Exception( |
183
|
|
|
'Column is already at the beginning of range (' . |
184
|
1 |
|
\PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($this->endColumn) . ' - ' . |
185
|
1 |
|
\PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($this->endColumn) . ')' |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
do { |
190
|
1 |
|
--$this->position; |
191
|
1 |
|
} while (($this->onlyExistingCells) && |
192
|
|
|
(!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) && |
193
|
|
|
($this->position >= $this->startColumn)); |
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Indicate if more columns exist in the worksheet range of columns that we're iterating. |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
5 |
|
public function valid() |
202
|
|
|
{ |
203
|
5 |
|
return $this->position <= $this->endColumn; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary. |
208
|
|
|
* |
209
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
210
|
|
|
*/ |
211
|
9 |
View Code Duplication |
protected function adjustForExistingOnlyRange() |
|
|
|
|
212
|
|
|
{ |
213
|
9 |
|
if ($this->onlyExistingCells) { |
214
|
|
|
while ((!$this->subject->cellExistsByColumnAndRow($this->startColumn, $this->rowIndex)) && |
215
|
|
|
($this->startColumn <= $this->endColumn)) { |
216
|
|
|
++$this->startColumn; |
217
|
|
|
} |
218
|
|
|
if ($this->startColumn > $this->endColumn) { |
219
|
|
|
throw new \PhpOffice\PhpSpreadsheet\Exception('No cells exist within the specified range'); |
220
|
|
|
} |
221
|
|
|
while ((!$this->subject->cellExistsByColumnAndRow($this->endColumn, $this->rowIndex)) && |
222
|
|
|
($this->endColumn >= $this->startColumn)) { |
223
|
|
|
--$this->endColumn; |
224
|
|
|
} |
225
|
|
|
if ($this->endColumn < $this->startColumn) { |
226
|
|
|
throw new \PhpOffice\PhpSpreadsheet\Exception('No cells exist within the specified range'); |
227
|
|
|
} |
228
|
|
|
} |
229
|
9 |
|
} |
230
|
|
|
} |
231
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.