1 | <?php |
||
23 | class RowIterator implements IteratorInterface |
||
24 | { |
||
25 | /** Definition of XML nodes names used to parse data */ |
||
26 | const XML_NODE_TABLE = 'table:table'; |
||
27 | const XML_NODE_ROW = 'table:table-row'; |
||
28 | const XML_NODE_CELL = 'table:table-cell'; |
||
29 | const MAX_COLUMNS_EXCEL = 16384; |
||
30 | |||
31 | /** Definition of XML attribute used to parse data */ |
||
32 | const XML_ATTRIBUTE_NUM_ROWS_REPEATED = 'table:number-rows-repeated'; |
||
33 | const XML_ATTRIBUTE_NUM_COLUMNS_REPEATED = 'table:number-columns-repeated'; |
||
34 | |||
35 | /** @var \Box\Spout\Reader\Wrapper\XMLReader The XMLReader object that will help read sheet's XML data */ |
||
36 | protected $xmlReader; |
||
37 | |||
38 | /** @var \Box\Spout\Reader\Common\XMLProcessor Helper Object to process XML nodes */ |
||
39 | protected $xmlProcessor; |
||
40 | |||
41 | /** @var bool Whether empty rows should be returned or skipped */ |
||
42 | protected $shouldPreserveEmptyRows; |
||
43 | |||
44 | /** @var Helper\CellValueFormatter Helper to format cell values */ |
||
45 | protected $cellValueFormatter; |
||
46 | |||
47 | /** @var RowManager Manages rows */ |
||
48 | protected $rowManager; |
||
49 | |||
50 | /** @var InternalEntityFactory Factory to create entities */ |
||
51 | protected $entityFactory; |
||
52 | |||
53 | /** @var bool Whether the iterator has already been rewound once */ |
||
54 | protected $hasAlreadyBeenRewound = false; |
||
55 | |||
56 | /** @var Row The currently processed row */ |
||
57 | protected $currentlyProcessedRow; |
||
58 | |||
59 | /** @var Row Buffer used to store the current row, while checking if there are more rows to read */ |
||
60 | protected $rowBuffer; |
||
61 | |||
62 | /** @var bool Indicates whether all rows have been read */ |
||
63 | protected $hasReachedEndOfFile = false; |
||
64 | |||
65 | /** @var int Last row index processed (one-based) */ |
||
66 | protected $lastRowIndexProcessed = 0; |
||
67 | |||
68 | /** @var int Row index to be processed next (one-based) */ |
||
69 | protected $nextRowIndexToBeProcessed = 1; |
||
70 | |||
71 | /** @var Cell Last processed cell (because when reading cell at column N+1, cell N is processed) */ |
||
72 | protected $lastProcessedCell; |
||
73 | |||
74 | /** @var int Number of times the last processed row should be repeated */ |
||
75 | protected $numRowsRepeated = 1; |
||
76 | |||
77 | /** @var int Number of times the last cell value should be copied to the cells on its right */ |
||
78 | protected $numColumnsRepeated = 1; |
||
79 | |||
80 | /** @var bool Whether at least one cell has been read for the row currently being processed */ |
||
81 | protected $hasAlreadyReadOneCellInCurrentRow = false; |
||
82 | |||
83 | /** |
||
84 | * @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element |
||
85 | * @param OptionsManagerInterface $optionsManager Reader's options manager |
||
86 | * @param CellValueFormatter $cellValueFormatter Helper to format cell values |
||
87 | * @param XMLProcessor $xmlProcessor Helper to process XML files |
||
88 | * @param RowManager $rowManager Manages rows |
||
89 | * @param InternalEntityFactory $entityFactory Factory to create entities |
||
90 | */ |
||
91 | 29 | public function __construct( |
|
112 | |||
113 | /** |
||
114 | * Rewind the Iterator to the first element. |
||
115 | * NOTE: It can only be done once, as it is not possible to read an XML file backwards. |
||
116 | * @see http://php.net/manual/en/iterator.rewind.php |
||
117 | * |
||
118 | * @throws \Box\Spout\Reader\Exception\IteratorNotRewindableException If the iterator is rewound more than once |
||
119 | * @return void |
||
120 | */ |
||
121 | 26 | public function rewind() |
|
138 | |||
139 | /** |
||
140 | * Checks if current position is valid |
||
141 | * @see http://php.net/manual/en/iterator.valid.php |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | 26 | public function valid() |
|
149 | |||
150 | /** |
||
151 | * Move forward to next element. Empty rows will be skipped. |
||
152 | * @see http://php.net/manual/en/iterator.next.php |
||
153 | * |
||
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 | * @return void |
||
157 | */ |
||
158 | 26 | public function next() |
|
166 | |||
167 | /** |
||
168 | * Returns whether we need data for the next row to be processed. |
||
169 | * We DO need to read data if: |
||
170 | * - we have not read any rows yet |
||
171 | * OR |
||
172 | * - the next row to be processed immediately follows the last read row |
||
173 | * |
||
174 | * @return bool Whether we need data for the next row to be processed. |
||
175 | */ |
||
176 | 26 | protected function doesNeedDataForNextRowToBeProcessed() |
|
185 | |||
186 | /** |
||
187 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
188 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
189 | * @return void |
||
190 | */ |
||
191 | 26 | protected function readDataForNextRow() |
|
203 | |||
204 | /** |
||
205 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-row>" starting node |
||
206 | * @return int A return code that indicates what action should the processor take next |
||
207 | */ |
||
208 | 26 | protected function processRowStartingNode($xmlReader) |
|
218 | |||
219 | /** |
||
220 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-cell>" starting node |
||
221 | * @return int A return code that indicates what action should the processor take next |
||
222 | */ |
||
223 | 26 | protected function processCellStartingNode($xmlReader) |
|
244 | |||
245 | /** |
||
246 | * @return int A return code that indicates what action should the processor take next |
||
247 | */ |
||
248 | 26 | protected function processRowEndingNode() |
|
282 | |||
283 | /** |
||
284 | * @return int A return code that indicates what action should the processor take next |
||
285 | */ |
||
286 | 24 | protected function processTableEndingNode() |
|
293 | |||
294 | /** |
||
295 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-row>" starting node |
||
296 | * @return int The value of "table:number-rows-repeated" attribute of the current node, or 1 if attribute missing |
||
297 | */ |
||
298 | 26 | protected function getNumRowsRepeatedForCurrentNode($xmlReader) |
|
304 | |||
305 | /** |
||
306 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<table:table-cell>" starting node |
||
307 | * @return int The value of "table:number-columns-repeated" attribute of the current node, or 1 if attribute missing |
||
308 | */ |
||
309 | 26 | protected function getNumColumnsRepeatedForCurrentNode($xmlReader) |
|
315 | |||
316 | /** |
||
317 | * Returns the cell with (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
318 | * |
||
319 | * @param \DOMNode $node |
||
320 | * @return Cell The cell set with the associated with the cell |
||
321 | */ |
||
322 | 26 | protected function getCell($node) |
|
334 | |||
335 | /** |
||
336 | * After finishing processing each cell, a row is considered empty if it contains |
||
337 | * no cells or if the last read cell is empty. |
||
338 | * After finishing processing each cell, the last read cell is not part of the |
||
339 | * row data yet (as we still need to apply the "num-columns-repeated" attribute). |
||
340 | * |
||
341 | * @param Row $currentRow |
||
342 | * @param Cell $lastReadCell The last read cell |
||
343 | * @return bool Whether the row is empty |
||
344 | */ |
||
345 | 26 | protected function isEmptyRow($currentRow, $lastReadCell) |
|
352 | |||
353 | /** |
||
354 | * Return the current element, from the buffer. |
||
355 | * @see http://php.net/manual/en/iterator.current.php |
||
356 | * |
||
357 | * @return Row |
||
358 | */ |
||
359 | 25 | public function current() |
|
363 | |||
364 | /** |
||
365 | * Return the key of the current element |
||
366 | * @see http://php.net/manual/en/iterator.key.php |
||
367 | * |
||
368 | * @return int |
||
369 | */ |
||
370 | 23 | public function key() |
|
374 | |||
375 | /** |
||
376 | * Cleans up what was created to iterate over the object. |
||
377 | * |
||
378 | * @return void |
||
379 | */ |
||
380 | public function end() |
||
384 | } |
||
385 |