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 $sheetDataXMLFilePath 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 | /** |
||
51 | * TODO: This variable can be deleted when row indices get preserved |
||
52 | * @var int Number of read rows |
||
53 | */ |
||
54 | protected $numReadRows = 0; |
||
55 | |||
56 | /** @var array Contains the data for the currently processed row (key = cell index, value = cell value) */ |
||
57 | protected $currentlyProcessedRowData = []; |
||
58 | |||
59 | /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ |
||
60 | protected $rowDataBuffer = null; |
||
61 | |||
62 | /** @var bool Indicates whether all rows have been read */ |
||
63 | protected $hasReachedEndOfFile = false; |
||
64 | |||
65 | /** @var int The number of columns the sheet has (0 meaning undefined) */ |
||
66 | protected $numColumns = 0; |
||
67 | |||
68 | /** @var bool Whether empty rows should be returned or skipped */ |
||
69 | protected $shouldPreserveEmptyRows; |
||
70 | |||
71 | /** @var int Last row index processed (one-based) */ |
||
72 | protected $lastRowIndexProcessed = 0; |
||
73 | |||
74 | /** @var int Row index to be processed next (one-based) */ |
||
75 | protected $nextRowIndexToBeProcessed = 0; |
||
76 | |||
77 | /** @var int Last column index processed (zero-based) */ |
||
78 | protected $lastColumnIndexProcessed = -1; |
||
79 | |||
80 | /** |
||
81 | * @param string $filePath Path of the XLSX file being read |
||
82 | * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml |
||
83 | * @param bool $shouldPreserveEmptyRows Whether empty rows should be preserved |
||
84 | * @param XMLReader $xmlReader XML Reader |
||
85 | * @param XMLProcessor $xmlProcessor Helper to process XML files |
||
86 | * @param CellValueFormatter $cellValueFormatter Helper to format cell values |
||
87 | */ |
||
88 | 33 | public function __construct($filePath, $sheetDataXMLFilePath, $shouldPreserveEmptyRows, $xmlReader, $xmlProcessor, $cellValueFormatter) |
|
104 | |||
105 | /** |
||
106 | * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml |
||
107 | * @return string Path of the XML file containing the sheet data, |
||
108 | * without the leading slash. |
||
109 | */ |
||
110 | 33 | protected function normalizeSheetDataXMLFilePath($sheetDataXMLFilePath) |
|
114 | |||
115 | /** |
||
116 | * Rewind the Iterator to the first element. |
||
117 | * Initializes the XMLReader object that reads the associated sheet data. |
||
118 | * The XMLReader is configured to be safe from billion laughs attack. |
||
119 | * @link http://php.net/manual/en/iterator.rewind.php |
||
120 | * |
||
121 | * @return void |
||
122 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data XML cannot be read |
||
123 | */ |
||
124 | 32 | public function rewind() |
|
141 | |||
142 | /** |
||
143 | * Checks if current position is valid |
||
144 | * @link http://php.net/manual/en/iterator.valid.php |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 31 | public function valid() |
|
152 | |||
153 | /** |
||
154 | * Move forward to next element. Reads data describing the next unprocessed row. |
||
155 | * @link http://php.net/manual/en/iterator.next.php |
||
156 | * |
||
157 | * @return void |
||
158 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
159 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
160 | */ |
||
161 | 31 | public function next() |
|
169 | |||
170 | /** |
||
171 | * Returns whether we need data for the next row to be processed. |
||
172 | * We don't need to read data if: |
||
173 | * we have already read at least one row |
||
174 | * AND |
||
175 | * we need to preserve empty rows |
||
176 | * AND |
||
177 | * the last row that was read is not the row that need to be processed |
||
178 | * (i.e. if we need to return empty rows) |
||
179 | * |
||
180 | * @return bool Whether we need data for the next row to be processed. |
||
181 | */ |
||
182 | 31 | protected function doesNeedDataForNextRowToBeProcessed() |
|
192 | |||
193 | /** |
||
194 | * @return void |
||
195 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
196 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
197 | */ |
||
198 | 31 | protected function readDataForNextRow() |
|
210 | |||
211 | /** |
||
212 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<dimension>" starting node |
||
213 | * @return int A return code that indicates what action should the processor take next |
||
214 | */ |
||
215 | 15 | protected function processDimensionStartingNode($xmlReader) |
|
225 | |||
226 | /** |
||
227 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<row>" starting node |
||
228 | * @return int A return code that indicates what action should the processor take next |
||
229 | */ |
||
230 | 30 | protected function processRowStartingNode($xmlReader) |
|
250 | |||
251 | /** |
||
252 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<cell>" starting node |
||
253 | * @return int A return code that indicates what action should the processor take next |
||
254 | */ |
||
255 | 30 | protected function processCellStartingNode($xmlReader) |
|
266 | |||
267 | /** |
||
268 | * @return int A return code that indicates what action should the processor take next |
||
269 | */ |
||
270 | 30 | protected function processRowEndingNode() |
|
289 | |||
290 | /** |
||
291 | * @return int A return code that indicates what action should the processor take next |
||
292 | */ |
||
293 | 30 | protected function processWorksheetEndingNode() |
|
300 | |||
301 | /** |
||
302 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<row>" node |
||
303 | * @return int Row index |
||
304 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
305 | */ |
||
306 | 30 | protected function getRowIndex($xmlReader) |
|
315 | |||
316 | /** |
||
317 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<c>" node |
||
318 | * @return int Column index |
||
319 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
320 | */ |
||
321 | 30 | protected function getColumnIndex($xmlReader) |
|
330 | |||
331 | /** |
||
332 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
333 | * |
||
334 | * @param \DOMNode $node |
||
335 | * @return string|int|float|bool|\DateTime|null The value associated with the cell (null when the cell has an error) |
||
336 | */ |
||
337 | 30 | protected function getCellValue($node) |
|
341 | |||
342 | /** |
||
343 | * @param array $rowData |
||
344 | * @return bool Whether the given row is empty |
||
345 | */ |
||
346 | 29 | protected function isEmptyRow($rowData) |
|
350 | |||
351 | /** |
||
352 | * Return the current element, either an empty row or from the buffer. |
||
353 | * @link http://php.net/manual/en/iterator.current.php |
||
354 | * |
||
355 | * @return array|null |
||
356 | */ |
||
357 | 30 | public function current() |
|
375 | |||
376 | /** |
||
377 | * Return the key of the current element. Here, the row index. |
||
378 | * @link http://php.net/manual/en/iterator.key.php |
||
379 | * |
||
380 | * @return int |
||
381 | */ |
||
382 | 29 | public function key() |
|
391 | |||
392 | |||
393 | /** |
||
394 | * Cleans up what was created to iterate over the object. |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | 32 | public function end() |
|
402 | } |
||
403 |