1 | <?php |
||
18 | class RowIterator implements IteratorInterface |
||
19 | { |
||
20 | /** |
||
21 | * Value passed to fgetcsv. 0 means "unlimited" (slightly slower but accomodates for very long lines). |
||
22 | */ |
||
23 | const MAX_READ_BYTES_PER_LINE = 0; |
||
24 | |||
25 | /** @var resource Pointer to the CSV file to read */ |
||
26 | protected $filePointer; |
||
27 | |||
28 | /** @var int Number of read rows */ |
||
29 | protected $numReadRows = 0; |
||
30 | |||
31 | /** @var Row|null Buffer used to store the current row, while checking if there are more rows to read */ |
||
32 | protected $rowBuffer; |
||
33 | |||
34 | /** @var bool Indicates whether all rows have been read */ |
||
35 | protected $hasReachedEndOfFile = false; |
||
36 | |||
37 | /** @var string Defines the character used to delimit fields (one character only) */ |
||
38 | protected $fieldDelimiter; |
||
39 | |||
40 | /** @var string Defines the character used to enclose fields (one character only) */ |
||
41 | protected $fieldEnclosure; |
||
42 | |||
43 | /** @var string Encoding of the CSV file to be read */ |
||
44 | protected $encoding; |
||
45 | |||
46 | /** @var bool Whether empty rows should be returned or skipped */ |
||
47 | protected $shouldPreserveEmptyRows; |
||
48 | |||
49 | /** @var \Box\Spout\Common\Helper\EncodingHelper Helper to work with different encodings */ |
||
50 | protected $encodingHelper; |
||
51 | |||
52 | /** @var \Box\Spout\Reader\CSV\Creator\InternalEntityFactory Factory to create entities */ |
||
53 | protected $entityFactory; |
||
54 | |||
55 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
56 | protected $globalFunctionsHelper; |
||
57 | |||
58 | /** @var OptionsManagerInterface */ |
||
59 | protected $optionsManager; |
||
60 | |||
61 | /** |
||
62 | * @param resource $filePointer Pointer to the CSV file to read |
||
63 | * @param OptionsManagerInterface $optionsManager |
||
64 | * @param EncodingHelper $encodingHelper |
||
65 | * @param InternalEntityFactory $entityFactory |
||
66 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
67 | */ |
||
68 | 32 | public function __construct( |
|
85 | |||
86 | /** |
||
87 | * Rewind the Iterator to the first element |
||
88 | * @see http://php.net/manual/en/iterator.rewind.php |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | 31 | public function rewind() |
|
101 | |||
102 | /** |
||
103 | * This rewinds and skips the BOM if inserted at the beginning of the file |
||
104 | * by moving the file pointer after it, so that it is not read. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | 31 | protected function rewindAndSkipBom() |
|
115 | |||
116 | /** |
||
117 | * Checks if current position is valid |
||
118 | * @see http://php.net/manual/en/iterator.valid.php |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | 29 | public function valid() |
|
126 | |||
127 | /** |
||
128 | * Move forward to next element. Reads data for the next unprocessed row. |
||
129 | * @see http://php.net/manual/en/iterator.next.php |
||
130 | * |
||
131 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
132 | * @return void |
||
133 | */ |
||
134 | 31 | public function next() |
|
142 | |||
143 | /** |
||
144 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
145 | * @return void |
||
146 | */ |
||
147 | 31 | protected function readDataForNextRow() |
|
164 | |||
165 | /** |
||
166 | * @param array|bool $currentRowData |
||
167 | * @return bool Whether the data for the current row can be returned or if we need to keep reading |
||
168 | */ |
||
169 | 29 | protected function shouldReadNextRow($currentRowData) |
|
180 | |||
181 | /** |
||
182 | * Returns the next row, converted if necessary to UTF-8. |
||
183 | * As fgetcsv() does not manage correctly encoding for non UTF-8 data, |
||
184 | * we remove manually whitespace with ltrim or rtrim (depending on the order of the bytes) |
||
185 | * @throws InvalidReaderOptionValueException |
||
186 | * @return array|false If unable to convert data to UTF-8 |
||
187 | */ |
||
188 | 31 | protected function getNextUTF8EncodedRow() |
|
241 | |||
242 | /** |
||
243 | * @param array|bool $lineData Array containing the cells value for the line |
||
244 | * @return bool Whether the given line is empty |
||
245 | */ |
||
246 | 29 | protected function isEmptyLine($lineData) |
|
250 | |||
251 | /** |
||
252 | * Return the current element from the buffer |
||
253 | * @see http://php.net/manual/en/iterator.current.php |
||
254 | * |
||
255 | * @return Row|null |
||
256 | */ |
||
257 | 27 | public function current() |
|
261 | |||
262 | /** |
||
263 | * Return the key of the current element |
||
264 | * @see http://php.net/manual/en/iterator.key.php |
||
265 | * |
||
266 | * @return int |
||
267 | */ |
||
268 | 21 | public function key() |
|
272 | |||
273 | /** |
||
274 | * Cleans up what was created to iterate over the object. |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | public function end() |
||
282 | } |
||
283 |