1 | <?php |
||
14 | class RowIterator implements IteratorInterface |
||
15 | { |
||
16 | /** |
||
17 | * If no value is given to fgetcsv(), it defaults to 8192 (which may be too low). |
||
18 | * Alignement with other functions like fgets() is discussed here: https://bugs.php.net/bug.php?id=48421 |
||
19 | */ |
||
20 | const MAX_READ_BYTES_PER_LINE = 32768; |
||
21 | |||
22 | /** @var resource Pointer to the CSV file to read */ |
||
23 | protected $filePointer; |
||
24 | |||
25 | /** @var int Number of read rows */ |
||
26 | protected $numReadRows = 0; |
||
27 | |||
28 | /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ |
||
29 | protected $rowDataBuffer = null; |
||
30 | |||
31 | /** @var bool Indicates whether all rows have been read */ |
||
32 | protected $hasReachedEndOfFile = false; |
||
33 | |||
34 | /** @var string Defines the character used to delimit fields (one character only) */ |
||
35 | protected $fieldDelimiter; |
||
36 | |||
37 | /** @var string Defines the character used to enclose fields (one character only) */ |
||
38 | protected $fieldEnclosure; |
||
39 | |||
40 | /** @var string Encoding of the CSV file to be read */ |
||
41 | protected $encoding; |
||
42 | |||
43 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
44 | protected $globalFunctionsHelper; |
||
45 | |||
46 | /** @var \Box\Spout\Common\Helper\EncodingHelper Helper to work with different encodings */ |
||
47 | protected $encodingHelper; |
||
48 | |||
49 | /** @var string End of line delimiter, encoded using the same encoding as the CSV */ |
||
50 | protected $encodedEOLDelimiter; |
||
51 | |||
52 | /** @var string End of line delimiter, given by the user as input. */ |
||
53 | protected $inputEOLDelimiter; |
||
54 | |||
55 | /** @var bool Whether empty rows should be returned or skipped */ |
||
56 | protected $shouldPreserveEmptyRows; |
||
57 | |||
58 | /** |
||
59 | * @param resource $filePointer Pointer to the CSV file to read |
||
60 | * @param string $fieldDelimiter Character that delimits fields |
||
61 | * @param string $fieldEnclosure Character that enclose fields |
||
62 | * @param string $endOfLineDelimiter End of line delimiter |
||
63 | * @param string $encoding Encoding of the CSV file to be read |
||
64 | * @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped |
||
65 | * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
||
66 | */ |
||
67 | 78 | public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $endOfLineDelimiter, $encoding, $shouldPreserveEmptyRows, $globalFunctionsHelper) |
|
79 | |||
80 | /** |
||
81 | * Rewind the Iterator to the first element |
||
82 | * @link http://php.net/manual/en/iterator.rewind.php |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | 78 | public function rewind() |
|
95 | |||
96 | /** |
||
97 | * This rewinds and skips the BOM if inserted at the beginning of the file |
||
98 | * by moving the file pointer after it, so that it is not read. |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | 78 | protected function rewindAndSkipBom() |
|
109 | |||
110 | /** |
||
111 | * Checks if current position is valid |
||
112 | * @link http://php.net/manual/en/iterator.valid.php |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 78 | public function valid() |
|
120 | |||
121 | /** |
||
122 | * Move forward to next element. Reads data for the next unprocessed row. |
||
123 | * @link http://php.net/manual/en/iterator.next.php |
||
124 | * |
||
125 | * @return void |
||
126 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
127 | */ |
||
128 | 78 | public function next() |
|
136 | |||
137 | /** |
||
138 | * @return void |
||
139 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
140 | */ |
||
141 | 78 | protected function readDataForNextRow() |
|
157 | |||
158 | /** |
||
159 | * @param array|bool $currentRowData |
||
160 | * @return bool Whether the data for the current row can be returned or if we need to keep reading |
||
161 | */ |
||
162 | 78 | protected function shouldReadNextRow($currentRowData) |
|
173 | |||
174 | /** |
||
175 | * Returns the next row, converted if necessary to UTF-8. |
||
176 | * As fgetcsv() does not manage correctly encoding for non UTF-8 data, |
||
177 | * we remove manually whitespace with ltrim or rtrim (depending on the order of the bytes) |
||
178 | * |
||
179 | * @return array|false The row for the current file pointer, encoded in UTF-8 or FALSE if nothing to read |
||
180 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
181 | */ |
||
182 | 78 | protected function getNextUTF8EncodedRow() |
|
209 | |||
210 | /** |
||
211 | * Returns the end of line delimiter, encoded using the same encoding as the CSV. |
||
212 | * The return value is cached. |
||
213 | * |
||
214 | * @return string |
||
1 ignored issue
–
show
|
|||
215 | */ |
||
216 | protected function getEncodedEOLDelimiter() |
||
224 | |||
225 | /** |
||
226 | * @param array|bool $lineData Array containing the cells value for the line |
||
227 | * @return bool Whether the given line is empty |
||
228 | */ |
||
229 | 78 | protected function isEmptyLine($lineData) |
|
233 | |||
234 | /** |
||
235 | * Return the current element from the buffer |
||
236 | * @link http://php.net/manual/en/iterator.current.php |
||
237 | * |
||
238 | * @return array|null |
||
239 | */ |
||
240 | 72 | public function current() |
|
244 | |||
245 | /** |
||
246 | * Return the key of the current element |
||
247 | * @link http://php.net/manual/en/iterator.key.php |
||
248 | * |
||
249 | * @return int |
||
250 | */ |
||
251 | 54 | public function key() |
|
255 | |||
256 | /** |
||
257 | * Cleans up what was created to iterate over the object. |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | public function end() |
||
265 | } |
||
266 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.