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 | 78 | 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 | 78 | 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 | 78 | 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 | 78 | public function next() |
|
131 | |||
132 | /** |
||
133 | * Returns whether we need data for the next row to be processed. |
||
134 | * We DO need to read data if: |
||
135 | * - we have not read any rows yet |
||
136 | * OR |
||
137 | * - the next row to be processed immediately follows the last read row |
||
138 | * |
||
139 | * @return bool Whether we need data for the next row to be processed. |
||
140 | */ |
||
141 | 78 | protected function doesNeedDataForNextRowToBeProcessed() |
|
150 | |||
151 | /** |
||
152 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object |
||
153 | * @return void |
||
154 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
155 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
156 | */ |
||
157 | 78 | protected function readDataForNextRow($xmlReader) |
|
196 | |||
197 | /** |
||
198 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-row>" starting node |
||
199 | * @return void |
||
200 | */ |
||
201 | 78 | protected function processRowStartingNode($xmlReader) |
|
209 | |||
210 | /** |
||
211 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-cell>" starting node |
||
212 | * @param array $rowData Data of all cells read so far |
||
213 | * @return array Original row data + data for the cell that was just read |
||
214 | */ |
||
215 | 78 | protected function processCellStartingNode($xmlReader, $rowData) |
|
235 | |||
236 | /** |
||
237 | * @param array $rowData Data of all cells read so far |
||
238 | * @param bool $isEmptyRow Whether the given row is empty |
||
239 | * @return array |
||
240 | */ |
||
241 | 75 | protected function processRowEndingNode($rowData, $isEmptyRow) |
|
264 | |||
265 | /** |
||
266 | * @return void |
||
267 | */ |
||
268 | 72 | protected function processTableEndingNode() |
|
273 | |||
274 | /** |
||
275 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-row>" starting node |
||
276 | * @return int The value of "table:number-rows-repeated" attribute of the current node, or 1 if attribute missing |
||
277 | */ |
||
278 | 78 | protected function getNumRowsRepeatedForCurrentNode($xmlReader) |
|
283 | |||
284 | /** |
||
285 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-cell>" starting node |
||
286 | * @return int The value of "table:number-columns-repeated" attribute of the current node, or 1 if attribute missing |
||
287 | */ |
||
288 | 78 | protected function getNumColumnsRepeatedForCurrentNode($xmlReader) |
|
293 | |||
294 | /** |
||
295 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
296 | * |
||
297 | * @param \DOMNode $node |
||
298 | * @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 |
||
299 | */ |
||
300 | 78 | protected function getCellValue($node) |
|
304 | |||
305 | /** |
||
306 | * After finishing processing each cell, a row is considered empty if it contains |
||
307 | * no cells or if the value of the last read cell is an empty string. |
||
308 | * After finishing processing each cell, the last read cell is not part of the |
||
309 | * row data yet (as we still need to apply the "num-columns-repeated" attribute). |
||
310 | * |
||
311 | * @param array $rowData |
||
312 | * @param string|int|float|bool|\DateTime|\DateInterval|null The value of the last read cell |
||
313 | * @return bool Whether the row is empty |
||
314 | */ |
||
315 | 78 | protected function isEmptyRow($rowData, $lastReadCellValue) |
|
322 | |||
323 | /** |
||
324 | * Return the current element, from the buffer. |
||
325 | * @link http://php.net/manual/en/iterator.current.php |
||
326 | * |
||
327 | * @return array|null |
||
328 | */ |
||
329 | 75 | public function current() |
|
333 | |||
334 | /** |
||
335 | * Return the key of the current element |
||
336 | * @link http://php.net/manual/en/iterator.key.php |
||
337 | * |
||
338 | * @return int |
||
339 | */ |
||
340 | 69 | public function key() |
|
344 | |||
345 | |||
346 | /** |
||
347 | * Cleans up what was created to iterate over the object. |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | public function end() |
||
355 | } |
||
356 |