1 | <?php |
||
17 | class RowIterator implements IteratorInterface |
||
18 | { |
||
19 | /** Definition of XML nodes names used to parse data */ |
||
20 | const XML_NODE_TABLE = 'table:table'; |
||
21 | const XML_NODE_ROW = 'table:table-row'; |
||
22 | const XML_NODE_CELL = 'table:table-cell'; |
||
23 | const MAX_COLUMNS_EXCEL = 16384; |
||
24 | |||
25 | /** Definition of XML attribute used to parse data */ |
||
26 | const XML_ATTRIBUTE_NUM_ROWS_REPEATED = 'table:number-rows-repeated'; |
||
27 | const XML_ATTRIBUTE_NUM_COLUMNS_REPEATED = 'table:number-columns-repeated'; |
||
28 | |||
29 | /** @var \Box\Spout\Reader\Wrapper\XMLReader The XMLReader object that will help read sheet's XML data */ |
||
30 | protected $xmlReader; |
||
31 | |||
32 | /** @var bool Whether empty rows should be returned or skipped */ |
||
33 | protected $shouldPreserveEmptyRows; |
||
34 | |||
35 | /** @var Helper\CellValueFormatter Helper to format cell values */ |
||
36 | protected $cellValueFormatter; |
||
37 | |||
38 | /** @var bool Whether the iterator has already been rewound once */ |
||
39 | protected $hasAlreadyBeenRewound = false; |
||
40 | |||
41 | /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ |
||
42 | protected $rowDataBuffer = null; |
||
43 | |||
44 | /** @var bool Indicates whether all rows have been read */ |
||
45 | protected $hasReachedEndOfFile = false; |
||
46 | |||
47 | /** @var int Last row index processed (one-based) */ |
||
48 | protected $lastRowIndexProcessed = 0; |
||
49 | |||
50 | /** @var int Row index to be processed next (one-based) */ |
||
51 | protected $nextRowIndexToBeProcessed = 1; |
||
52 | |||
53 | /** @var mixed|null Value of the last processed cell (because when reading cell at column N+1, cell N is processed) */ |
||
54 | protected $lastProcessedCellValue = null; |
||
55 | |||
56 | /** @var int Number of times the last processed row should be repeated */ |
||
57 | protected $numRowsRepeated = 1; |
||
58 | |||
59 | /** @var int Number of times the last cell value should be copied to the cells on its right */ |
||
60 | protected $numColumnsRepeated = 1; |
||
61 | |||
62 | /** @var bool Whether at least one cell has been read for the row currently being processed */ |
||
63 | protected $hasAlreadyReadOneCellInCurrentRow = false; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element |
||
68 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
69 | * @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped |
||
70 | */ |
||
71 | 75 | public function __construct($xmlReader, $shouldFormatDates, $shouldPreserveEmptyRows) |
|
77 | |||
78 | /** |
||
79 | * Rewind the Iterator to the first element. |
||
80 | * NOTE: It can only be done once, as it is not possible to read an XML file backwards. |
||
81 | * @link http://php.net/manual/en/iterator.rewind.php |
||
82 | * |
||
83 | * @return void |
||
84 | * @throws \Box\Spout\Reader\Exception\IteratorNotRewindableException If the iterator is rewound more than once |
||
85 | */ |
||
86 | 75 | public function rewind() |
|
103 | |||
104 | /** |
||
105 | * Checks if current position is valid |
||
106 | * @link http://php.net/manual/en/iterator.valid.php |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | 75 | public function valid() |
|
114 | |||
115 | /** |
||
116 | * Move forward to next element. Empty rows will be skipped. |
||
117 | * @link http://php.net/manual/en/iterator.next.php |
||
118 | * |
||
119 | * @return void |
||
120 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
121 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
122 | */ |
||
123 | 75 | public function next() |
|
131 | |||
132 | /** |
||
133 | * Returns whether we need data for the next row to be processed. |
||
134 | * We don't need to read data if: |
||
135 | * we have already read at least one row |
||
136 | * AND |
||
137 | * we need to preserve empty rows |
||
138 | * AND |
||
139 | * the last row that was read is not the row that need to be processed |
||
140 | * (i.e. if we need to return empty rows) |
||
141 | * |
||
142 | * @return bool Whether we need data for the next row to be processed. |
||
143 | */ |
||
144 | 75 | protected function doesNeedDataForNextRowToBeProcessed() |
|
154 | |||
155 | /** |
||
156 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object |
||
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 | 75 | protected function readDataForNextRow($xmlReader) |
|
200 | |||
201 | /** |
||
202 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-row>" starting node |
||
203 | * @return void |
||
204 | */ |
||
205 | 75 | protected function processRowStartingNode($xmlReader) |
|
213 | |||
214 | /** |
||
215 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-cell>" starting node |
||
216 | * @param array $rowData Data of all cells read so far |
||
217 | * @return array Original row data + data for the cell that was just read |
||
218 | */ |
||
219 | 75 | protected function processCellStartingNode($xmlReader, $rowData) |
|
239 | |||
240 | /** |
||
241 | * @param array $rowData Data of all cells read so far |
||
242 | * @param bool $isEmptyRow Whether the given row is empty |
||
243 | * @return array |
||
244 | */ |
||
245 | 72 | protected function processRowEndingNode($rowData, $isEmptyRow) |
|
268 | |||
269 | /** |
||
270 | * @return void |
||
271 | */ |
||
272 | 69 | protected function processTableEndingNode() |
|
277 | |||
278 | /** |
||
279 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-row>" starting node |
||
280 | * @return int The value of "table:number-rows-repeated" attribute of the current node, or 1 if attribute missing |
||
281 | */ |
||
282 | 75 | protected function getNumRowsRepeatedForCurrentNode($xmlReader) |
|
287 | |||
288 | /** |
||
289 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-cell>" starting node |
||
290 | * @return int The value of "table:number-columns-repeated" attribute of the current node, or 1 if attribute missing |
||
291 | */ |
||
292 | 75 | protected function getNumColumnsRepeatedForCurrentNode($xmlReader) |
|
297 | |||
298 | /** |
||
299 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
300 | * |
||
301 | * @param \DOMNode $node |
||
302 | * @return string|int|float|bool|\DateTime|\DateInterval|null The value associated with the cell, empty string if cell's type is void/undefined, null on error |
||
303 | */ |
||
304 | 75 | protected function getCellValue($node) |
|
308 | |||
309 | /** |
||
310 | * After finishing processing each cell, a row is considered empty if it contains |
||
311 | * no cells or if the value of the last read cell is an empty string. |
||
312 | * After finishing processing each cell, the last read cell is not part of the |
||
313 | * row data yet (as we still need to apply the "num-columns-repeated" attribute). |
||
314 | * |
||
315 | * @param array $rowData |
||
316 | * @param string|int|float|bool|\DateTime|\DateInterval|null The value of the last read cell |
||
317 | * @return bool Whether the row is empty |
||
318 | */ |
||
319 | 75 | protected function isEmptyRow($rowData, $lastReadCellValue) |
|
326 | |||
327 | /** |
||
328 | * Return the current element, from the buffer. |
||
329 | * @link http://php.net/manual/en/iterator.current.php |
||
330 | * |
||
331 | * @return array|null |
||
332 | */ |
||
333 | 72 | public function current() |
|
337 | |||
338 | /** |
||
339 | * Return the key of the current element |
||
340 | * @link http://php.net/manual/en/iterator.key.php |
||
341 | * |
||
342 | * @return int |
||
343 | */ |
||
344 | 66 | public function key() |
|
348 | |||
349 | |||
350 | /** |
||
351 | * Cleans up what was created to iterate over the object. |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function end() |
||
359 | } |
||
360 |