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 Row|null Buffer used to store the current row, while checking if there are more rows to read */ |
||
31 | protected $rowBuffer; |
||
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\EncodingHelper Helper to work with different encodings */ |
||
49 | protected $encodingHelper; |
||
50 | |||
51 | /** @var \Box\Spout\Reader\CSV\Creator\InternalEntityFactory Factory to create entities */ |
||
52 | protected $entityFactory; |
||
53 | |||
54 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
55 | protected $globalFunctionsHelper; |
||
56 | |||
57 | /** |
||
58 | * @param resource $filePointer Pointer to the CSV file to read |
||
59 | * @param OptionsManagerInterface $optionsManager |
||
60 | * @param EncodingHelper $encodingHelper |
||
61 | * @param InternalEntityFactory $entityFactory |
||
62 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
63 | */ |
||
64 | 27 | public function __construct( |
|
80 | |||
81 | /** |
||
82 | * Rewind the Iterator to the first element |
||
83 | * @see http://php.net/manual/en/iterator.rewind.php |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | 26 | public function rewind() |
|
96 | |||
97 | /** |
||
98 | * This rewinds and skips the BOM if inserted at the beginning of the file |
||
99 | * by moving the file pointer after it, so that it is not read. |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | 26 | protected function rewindAndSkipBom() |
|
110 | |||
111 | /** |
||
112 | * Checks if current position is valid |
||
113 | * @see http://php.net/manual/en/iterator.valid.php |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 26 | public function valid() |
|
121 | |||
122 | /** |
||
123 | * Move forward to next element. Reads data for the next unprocessed row. |
||
124 | * @see http://php.net/manual/en/iterator.next.php |
||
125 | * |
||
126 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
127 | * @return void |
||
128 | */ |
||
129 | 26 | public function next() |
|
137 | |||
138 | /** |
||
139 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
140 | * @return void |
||
141 | */ |
||
142 | 26 | protected function readDataForNextRow() |
|
159 | |||
160 | /** |
||
161 | * @param array|bool $currentRowData |
||
162 | * @return bool Whether the data for the current row can be returned or if we need to keep reading |
||
163 | */ |
||
164 | 26 | protected function shouldReadNextRow($currentRowData) |
|
175 | |||
176 | /** |
||
177 | * Returns the next row, converted if necessary to UTF-8. |
||
178 | * As fgetcsv() does not manage correctly encoding for non UTF-8 data, |
||
179 | * we remove manually whitespace with ltrim or rtrim (depending on the order of the bytes) |
||
180 | * |
||
181 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
182 | * @return array|false The row for the current file pointer, encoded in UTF-8 or FALSE if nothing to read |
||
183 | */ |
||
184 | 26 | protected function getNextUTF8EncodedRow() |
|
211 | |||
212 | /** |
||
213 | * @param array|bool $lineData Array containing the cells value for the line |
||
214 | * @return bool Whether the given line is empty |
||
215 | */ |
||
216 | 26 | protected function isEmptyLine($lineData) |
|
220 | |||
221 | /** |
||
222 | * Return the current element from the buffer |
||
223 | * @see http://php.net/manual/en/iterator.current.php |
||
224 | * |
||
225 | * @return Row|null |
||
226 | */ |
||
227 | 24 | public function current() |
|
231 | |||
232 | /** |
||
233 | * Return the key of the current element |
||
234 | * @see http://php.net/manual/en/iterator.key.php |
||
235 | * |
||
236 | * @return int |
||
237 | */ |
||
238 | 18 | public function key() |
|
242 | |||
243 | /** |
||
244 | * Cleans up what was created to iterate over the object. |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | public function end() |
||
252 | } |
||
253 |