1 | <?php |
||
17 | class RowIterator implements IteratorInterface |
||
18 | { |
||
19 | /** |
||
20 | * Value passed to fgetcsv. 0 means "unlimited" (slightly slower but accomodates for very long lines). |
||
21 | */ |
||
22 | const MAX_READ_BYTES_PER_LINE = 0; |
||
23 | |||
24 | /** @var resource Pointer to the CSV file to read */ |
||
25 | protected $filePointer; |
||
26 | |||
27 | /** @var int Number of read rows */ |
||
28 | protected $numReadRows = 0; |
||
29 | |||
30 | /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ |
||
31 | protected $rowDataBuffer = null; |
||
32 | |||
33 | /** @var bool Indicates whether all rows have been read */ |
||
34 | protected $hasReachedEndOfFile = false; |
||
35 | |||
36 | /** @var string Defines the character used to delimit fields (one character only) */ |
||
37 | protected $fieldDelimiter; |
||
38 | |||
39 | /** @var string Defines the character used to enclose fields (one character only) */ |
||
40 | protected $fieldEnclosure; |
||
41 | |||
42 | /** @var string Encoding of the CSV file to be read */ |
||
43 | protected $encoding; |
||
44 | |||
45 | /** @var bool Whether empty rows should be returned or skipped */ |
||
46 | protected $shouldPreserveEmptyRows; |
||
47 | |||
48 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
49 | protected $globalFunctionsHelper; |
||
50 | |||
51 | /** @var \Box\Spout\Common\Helper\EncodingHelper Helper to work with different encodings */ |
||
52 | protected $encodingHelper; |
||
53 | |||
54 | /** |
||
55 | * @param resource $filePointer Pointer to the CSV file to read |
||
56 | * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager |
||
57 | * @param \Box\Spout\Common\Helper\EncodingHelper $encodingHelper |
||
58 | * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
||
59 | */ |
||
60 | 26 | public function __construct($filePointer, $optionsManager, $encodingHelper, $globalFunctionsHelper) |
|
70 | |||
71 | /** |
||
72 | * Rewind the Iterator to the first element |
||
73 | * @link http://php.net/manual/en/iterator.rewind.php |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 25 | public function rewind() |
|
86 | |||
87 | /** |
||
88 | * This rewinds and skips the BOM if inserted at the beginning of the file |
||
89 | * by moving the file pointer after it, so that it is not read. |
||
90 | * |
||
91 | * @return void |
||
92 | */ |
||
93 | 25 | protected function rewindAndSkipBom() |
|
100 | |||
101 | /** |
||
102 | * Checks if current position is valid |
||
103 | * @link http://php.net/manual/en/iterator.valid.php |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 25 | public function valid() |
|
111 | |||
112 | /** |
||
113 | * Move forward to next element. Reads data for the next unprocessed row. |
||
114 | * @link http://php.net/manual/en/iterator.next.php |
||
115 | * |
||
116 | * @return void |
||
117 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
118 | */ |
||
119 | 25 | public function next() |
|
127 | |||
128 | /** |
||
129 | * @return void |
||
130 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
131 | */ |
||
132 | 25 | protected function readDataForNextRow() |
|
148 | |||
149 | /** |
||
150 | * @param array|bool $currentRowData |
||
151 | * @return bool Whether the data for the current row can be returned or if we need to keep reading |
||
152 | */ |
||
153 | 25 | protected function shouldReadNextRow($currentRowData) |
|
164 | |||
165 | /** |
||
166 | * Returns the next row, converted if necessary to UTF-8. |
||
167 | * As fgetcsv() does not manage correctly encoding for non UTF-8 data, |
||
168 | * we remove manually whitespace with ltrim or rtrim (depending on the order of the bytes) |
||
169 | * |
||
170 | * @return array|false The row for the current file pointer, encoded in UTF-8 or FALSE if nothing to read |
||
171 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
172 | */ |
||
173 | 25 | protected function getNextUTF8EncodedRow() |
|
200 | |||
201 | /** |
||
202 | * @param array|bool $lineData Array containing the cells value for the line |
||
203 | * @return bool Whether the given line is empty |
||
204 | */ |
||
205 | 25 | protected function isEmptyLine($lineData) |
|
209 | |||
210 | /** |
||
211 | * Return the current element from the buffer |
||
212 | * @link http://php.net/manual/en/iterator.current.php |
||
213 | * |
||
214 | * @return array|null |
||
215 | */ |
||
216 | 23 | public function current() |
|
220 | |||
221 | /** |
||
222 | * Return the key of the current element |
||
223 | * @link http://php.net/manual/en/iterator.key.php |
||
224 | * |
||
225 | * @return int |
||
226 | */ |
||
227 | 17 | public function key() |
|
231 | |||
232 | /** |
||
233 | * Cleans up what was created to iterate over the object. |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | public function end() |
||
241 | } |
||
242 |