1 | <?php |
||
21 | class RowIterator implements IteratorInterface |
||
22 | { |
||
23 | /** Definition of XML nodes names used to parse data */ |
||
24 | const XML_NODE_DIMENSION = 'dimension'; |
||
25 | const XML_NODE_WORKSHEET = 'worksheet'; |
||
26 | const XML_NODE_ROW = 'row'; |
||
27 | const XML_NODE_CELL = 'c'; |
||
28 | |||
29 | /** Definition of XML attributes used to parse data */ |
||
30 | const XML_ATTRIBUTE_REF = 'ref'; |
||
31 | const XML_ATTRIBUTE_SPANS = 'spans'; |
||
32 | const XML_ATTRIBUTE_ROW_INDEX = 'r'; |
||
33 | const XML_ATTRIBUTE_CELL_INDEX = 'r'; |
||
34 | |||
35 | /** @var string Path of the XLSX file being read */ |
||
36 | protected $filePath; |
||
37 | |||
38 | /** @var string Path of the sheet data XML file as in [Content_Types].xml */ |
||
39 | protected $sheetDataXMLFilePath; |
||
40 | |||
41 | /** @var \Box\Spout\Reader\Wrapper\XMLReader The XMLReader object that will help read sheet's XML data */ |
||
42 | protected $xmlReader; |
||
43 | |||
44 | /** @var \Box\Spout\Reader\Common\XMLProcessor Helper Object to process XML nodes */ |
||
45 | protected $xmlProcessor; |
||
46 | |||
47 | /** @var Helper\CellValueFormatter Helper to format cell values */ |
||
48 | protected $cellValueFormatter; |
||
49 | |||
50 | /** @var \Box\Spout\Reader\Common\Manager\RowManager Manages rows */ |
||
51 | protected $rowManager; |
||
52 | |||
53 | /** @var \Box\Spout\Reader\XLSX\Creator\InternalEntityFactory Factory to create entities */ |
||
54 | protected $entityFactory; |
||
55 | |||
56 | /** |
||
57 | * TODO: This variable can be deleted when row indices get preserved |
||
58 | * @var int Number of read rows |
||
59 | */ |
||
60 | protected $numReadRows = 0; |
||
61 | |||
62 | /** @var Row Contains the row currently processed */ |
||
63 | protected $currentlyProcessedRow; |
||
64 | |||
65 | /** @var Row|null Buffer used to store the current row, while checking if there are more rows to read */ |
||
66 | protected $rowBuffer; |
||
67 | |||
68 | /** @var bool Indicates whether all rows have been read */ |
||
69 | protected $hasReachedEndOfFile = false; |
||
70 | |||
71 | /** @var int The number of columns the sheet has (0 meaning undefined) */ |
||
72 | protected $numColumns = 0; |
||
73 | |||
74 | /** @var bool Whether empty rows should be returned or skipped */ |
||
75 | protected $shouldPreserveEmptyRows; |
||
76 | |||
77 | /** @var int Last row index processed (one-based) */ |
||
78 | protected $lastRowIndexProcessed = 0; |
||
79 | |||
80 | /** @var int Row index to be processed next (one-based) */ |
||
81 | protected $nextRowIndexToBeProcessed = 0; |
||
82 | |||
83 | /** @var int Last column index processed (zero-based) */ |
||
84 | protected $lastColumnIndexProcessed = -1; |
||
85 | |||
86 | /** |
||
87 | * @param string $filePath Path of the XLSX file being read |
||
88 | * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml |
||
89 | * @param bool $shouldPreserveEmptyRows Whether empty rows should be preserved |
||
90 | * @param XMLReader $xmlReader XML Reader |
||
91 | * @param XMLProcessor $xmlProcessor Helper to process XML files |
||
92 | * @param CellValueFormatter $cellValueFormatter Helper to format cell values |
||
93 | * @param RowManager $rowManager Manages rows |
||
94 | * @param InternalEntityFactory $entityFactory Factory to create entities |
||
95 | */ |
||
96 | 41 | public function __construct( |
|
122 | |||
123 | /** |
||
124 | * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml |
||
125 | * @return string Path of the XML file containing the sheet data, |
||
126 | * without the leading slash. |
||
127 | */ |
||
128 | 41 | protected function normalizeSheetDataXMLFilePath($sheetDataXMLFilePath) |
|
132 | |||
133 | /** |
||
134 | * Rewind the Iterator to the first element. |
||
135 | * Initializes the XMLReader object that reads the associated sheet data. |
||
136 | * The XMLReader is configured to be safe from billion laughs attack. |
||
137 | * @see http://php.net/manual/en/iterator.rewind.php |
||
138 | * |
||
139 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data XML cannot be read |
||
140 | * @return void |
||
141 | */ |
||
142 | 39 | public function rewind() |
|
159 | |||
160 | /** |
||
161 | * Checks if current position is valid |
||
162 | * @see http://php.net/manual/en/iterator.valid.php |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | 38 | public function valid() |
|
170 | |||
171 | /** |
||
172 | * Move forward to next element. Reads data describing the next unprocessed row. |
||
173 | * @see http://php.net/manual/en/iterator.next.php |
||
174 | * |
||
175 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
176 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
177 | * @return void |
||
178 | */ |
||
179 | 38 | public function next() |
|
187 | |||
188 | /** |
||
189 | * Returns whether we need data for the next row to be processed. |
||
190 | * We don't need to read data if: |
||
191 | * we have already read at least one row |
||
192 | * AND |
||
193 | * we need to preserve empty rows |
||
194 | * AND |
||
195 | * the last row that was read is not the row that need to be processed |
||
196 | * (i.e. if we need to return empty rows) |
||
197 | * |
||
198 | * @return bool Whether we need data for the next row to be processed. |
||
199 | */ |
||
200 | 38 | protected function doesNeedDataForNextRowToBeProcessed() |
|
210 | |||
211 | /** |
||
212 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
213 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
214 | * @return void |
||
215 | */ |
||
216 | 38 | protected function readDataForNextRow() |
|
228 | |||
229 | /** |
||
230 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<dimension>" starting node |
||
231 | * @return int A return code that indicates what action should the processor take next |
||
232 | */ |
||
233 | 17 | protected function processDimensionStartingNode($xmlReader) |
|
243 | |||
244 | /** |
||
245 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<row>" starting node |
||
246 | * @return int A return code that indicates what action should the processor take next |
||
247 | */ |
||
248 | 37 | protected function processRowStartingNode($xmlReader) |
|
269 | |||
270 | /** |
||
271 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<cell>" starting node |
||
272 | * @return int A return code that indicates what action should the processor take next |
||
273 | */ |
||
274 | 37 | protected function processCellStartingNode($xmlReader) |
|
287 | |||
288 | /** |
||
289 | * @return int A return code that indicates what action should the processor take next |
||
290 | */ |
||
291 | 37 | protected function processRowEndingNode() |
|
310 | |||
311 | /** |
||
312 | * @return int A return code that indicates what action should the processor take next |
||
313 | */ |
||
314 | 37 | protected function processWorksheetEndingNode() |
|
321 | |||
322 | /** |
||
323 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<row>" node |
||
324 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
325 | * @return int Row index |
||
326 | */ |
||
327 | 37 | protected function getRowIndex($xmlReader) |
|
336 | |||
337 | /** |
||
338 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<c>" node |
||
339 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
340 | * @return int Column index |
||
341 | */ |
||
342 | 37 | protected function getColumnIndex($xmlReader) |
|
351 | |||
352 | /** |
||
353 | * Returns the cell with (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
354 | * |
||
355 | * @param \DOMNode $node |
||
356 | * @return Cell The cell set with the associated with the cell |
||
357 | */ |
||
358 | 37 | protected function getCell($node) |
|
370 | |||
371 | /** |
||
372 | * Return the current element, either an empty row or from the buffer. |
||
373 | * @see http://php.net/manual/en/iterator.current.php |
||
374 | * |
||
375 | * @return Row|null |
||
376 | */ |
||
377 | 37 | public function current() |
|
395 | |||
396 | /** |
||
397 | * Return the key of the current element. Here, the row index. |
||
398 | * @see http://php.net/manual/en/iterator.key.php |
||
399 | * |
||
400 | * @return int |
||
401 | */ |
||
402 | 36 | public function key() |
|
411 | |||
412 | /** |
||
413 | * Cleans up what was created to iterate over the object. |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | 40 | public function end() |
|
421 | } |
||
422 |