1 | <?php |
||
14 | class RowIterator implements IteratorInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var int Number of bytes to read |
||
18 | */ |
||
19 | protected $maxReadBytesPerLine; |
||
20 | |||
21 | /** @var resource Pointer to the CSV file to read */ |
||
22 | protected $filePointer; |
||
23 | |||
24 | /** @var int Number of read rows */ |
||
25 | protected $numReadRows = 0; |
||
26 | |||
27 | /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ |
||
28 | protected $rowDataBuffer = null; |
||
29 | |||
30 | /** @var bool Indicates whether all rows have been read */ |
||
31 | protected $hasReachedEndOfFile = false; |
||
32 | |||
33 | /** @var string Defines the character used to delimit fields (one character only) */ |
||
34 | protected $fieldDelimiter; |
||
35 | |||
36 | /** @var string Defines the character used to enclose fields (one character only) */ |
||
37 | protected $fieldEnclosure; |
||
38 | |||
39 | /** @var string Encoding of the CSV file to be read */ |
||
40 | protected $encoding; |
||
41 | |||
42 | /** @var string End of line delimiter, given by the user as input. */ |
||
43 | protected $inputEOLDelimiter; |
||
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 | /** @var string End of line delimiter, encoded using the same encoding as the CSV */ |
||
55 | protected $encodedEOLDelimiter; |
||
56 | |||
57 | /** |
||
58 | * @param resource $filePointer Pointer to the CSV file to read |
||
59 | * @param \Box\Spout\Reader\CSV\ReaderOptions $options |
||
60 | * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
||
61 | */ |
||
62 | 81 | public function __construct($filePointer, $options, $globalFunctionsHelper) |
|
63 | { |
||
64 | 81 | $this->filePointer = $filePointer; |
|
65 | 81 | $this->fieldDelimiter = $options->getFieldDelimiter(); |
|
66 | 81 | $this->fieldEnclosure = $options->getFieldEnclosure(); |
|
67 | 81 | $this->encoding = $options->getEncoding(); |
|
68 | 81 | $this->inputEOLDelimiter = $options->getEndOfLineCharacter(); |
|
69 | 81 | $this->maxReadBytesPerLine = $options->getMaxReadBytesPerLine(); |
|
70 | 81 | $this->shouldPreserveEmptyRows = $options->shouldPreserveEmptyRows(); |
|
71 | 81 | $this->globalFunctionsHelper = $globalFunctionsHelper; |
|
72 | |||
73 | 81 | $this->encodingHelper = new EncodingHelper($globalFunctionsHelper); |
|
74 | 81 | } |
|
75 | |||
76 | /** |
||
77 | * Rewind the Iterator to the first element |
||
78 | * @link http://php.net/manual/en/iterator.rewind.php |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | 81 | public function rewind() |
|
91 | |||
92 | /** |
||
93 | * This rewinds and skips the BOM if inserted at the beginning of the file |
||
94 | * by moving the file pointer after it, so that it is not read. |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | 81 | protected function rewindAndSkipBom() |
|
105 | |||
106 | /** |
||
107 | * Checks if current position is valid |
||
108 | * @link http://php.net/manual/en/iterator.valid.php |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 81 | public function valid() |
|
116 | |||
117 | /** |
||
118 | * Move forward to next element. Reads data for the next unprocessed row. |
||
119 | * @link http://php.net/manual/en/iterator.next.php |
||
120 | * |
||
121 | * @return void |
||
122 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
123 | */ |
||
124 | 81 | public function next() |
|
132 | |||
133 | /** |
||
134 | * @return void |
||
135 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
136 | */ |
||
137 | 81 | protected function readDataForNextRow() |
|
153 | |||
154 | /** |
||
155 | * @param array|bool $currentRowData |
||
156 | * @return bool Whether the data for the current row can be returned or if we need to keep reading |
||
157 | */ |
||
158 | 81 | protected function shouldReadNextRow($currentRowData) |
|
169 | |||
170 | /** |
||
171 | * Returns the next row, converted if necessary to UTF-8. |
||
172 | * As fgetcsv() does not manage correctly encoding for non UTF-8 data, |
||
173 | * we remove manually whitespace with ltrim or rtrim (depending on the order of the bytes) |
||
174 | * |
||
175 | * @return array|false The row for the current file pointer, encoded in UTF-8 or FALSE if nothing to read |
||
176 | * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 |
||
177 | */ |
||
178 | 81 | protected function getNextUTF8EncodedRow() |
|
205 | |||
206 | /** |
||
207 | * Returns the end of line delimiter, encoded using the same encoding as the CSV. |
||
208 | * The return value is cached. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function getEncodedEOLDelimiter() |
||
220 | |||
221 | /** |
||
222 | * @param array|bool $lineData Array containing the cells value for the line |
||
223 | * @return bool Whether the given line is empty |
||
224 | */ |
||
225 | 81 | protected function isEmptyLine($lineData) |
|
229 | |||
230 | /** |
||
231 | * Return the current element from the buffer |
||
232 | * @link http://php.net/manual/en/iterator.current.php |
||
233 | * |
||
234 | * @return array|null |
||
235 | */ |
||
236 | 75 | public function current() |
|
240 | |||
241 | /** |
||
242 | * Return the key of the current element |
||
243 | * @link http://php.net/manual/en/iterator.key.php |
||
244 | * |
||
245 | * @return int |
||
246 | */ |
||
247 | 57 | public function key() |
|
251 | |||
252 | /** |
||
253 | * Cleans up what was created to iterate over the object. |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | public function end() |
||
261 | } |
||
262 |