Failed Conditions
Pull Request — develop_3.0 (#507)
by Adrien
02:47
created

RowIterator::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Box\Spout\Reader\CSV;
4
5
use Box\Spout\Common\Helper\EncodingHelper;
6
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
7
use Box\Spout\Common\Manager\OptionsManagerInterface;
8
use Box\Spout\Reader\Common\Entity\Options;
9
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory;
10
use Box\Spout\Reader\IteratorInterface;
11
12
/**
13
 * Class RowIterator
14
 * Iterate over CSV rows.
15
 */
16
class RowIterator implements IteratorInterface
17
{
18
    /**
19
     * Value passed to fgetcsv. 0 means "unlimited" (slightly slower but accomodates for very long lines).
20
     */
21
    const MAX_READ_BYTES_PER_LINE = 0;
22
23
    /** @var resource Pointer to the CSV file to read */
24
    protected $filePointer;
25
26
    /** @var int Number of read rows */
27
    protected $numReadRows = 0;
28
29
    /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */
30
    protected $rowDataBuffer;
31
32
    /** @var bool Indicates whether all rows have been read */
33
    protected $hasReachedEndOfFile = false;
34
35
    /** @var string Defines the character used to delimit fields (one character only) */
36
    protected $fieldDelimiter;
37
38
    /** @var string Defines the character used to enclose fields (one character only) */
39
    protected $fieldEnclosure;
40
41
    /** @var string Encoding of the CSV file to be read */
42
    protected $encoding;
43
44
    /** @var bool Whether empty rows should be returned or skipped */
45
    protected $shouldPreserveEmptyRows;
46
47
    /** @var \Box\Spout\Common\Helper\EncodingHelper Helper to work with different encodings */
48
    protected $encodingHelper;
49
50
    /** @var \Box\Spout\Reader\CSV\Creator\InternalEntityFactory Factory to create entities */
51
    protected $entityFactory;
52
53
    /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
54
    protected $globalFunctionsHelper;
55
56
    /**
57
     * @param resource $filePointer Pointer to the CSV file to read
58
     * @param OptionsManagerInterface $optionsManager
59
     * @param EncodingHelper $encodingHelper
60
     * @param InternalEntityFactory $entityFactory
61
     * @param GlobalFunctionsHelper $globalFunctionsHelper
62
     */
63 27
    public function __construct(
64
        $filePointer,
65
        OptionsManagerInterface $optionsManager,
66
        EncodingHelper $encodingHelper,
67
        InternalEntityFactory $entityFactory,
68
        GlobalFunctionsHelper $globalFunctionsHelper
69
    ) {
70 27
        $this->filePointer = $filePointer;
71 27
        $this->fieldDelimiter = $optionsManager->getOption(Options::FIELD_DELIMITER);
72 27
        $this->fieldEnclosure = $optionsManager->getOption(Options::FIELD_ENCLOSURE);
73 27
        $this->encoding = $optionsManager->getOption(Options::ENCODING);
74 27
        $this->shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
75 27
        $this->encodingHelper = $encodingHelper;
76 27
        $this->entityFactory = $entityFactory;
77 27
        $this->globalFunctionsHelper = $globalFunctionsHelper;
78 27
    }
79
80
    /**
81
     * Rewind the Iterator to the first element
82
     * @see http://php.net/manual/en/iterator.rewind.php
83
     *
84
     * @return void
85
     */
86 26
    public function rewind()
87
    {
88 26
        $this->rewindAndSkipBom();
89
90 26
        $this->numReadRows = 0;
91 26
        $this->rowDataBuffer = null;
92
93 26
        $this->next();
94 26
    }
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 26
    protected function rewindAndSkipBom()
103
    {
104 26
        $byteOffsetToSkipBom = $this->encodingHelper->getBytesOffsetToSkipBOM($this->filePointer, $this->encoding);
105
106
        // sets the cursor after the BOM (0 means no BOM, so rewind it)
107 26
        $this->globalFunctionsHelper->fseek($this->filePointer, $byteOffsetToSkipBom);
108 26
    }
109
110
    /**
111
     * Checks if current position is valid
112
     * @see http://php.net/manual/en/iterator.valid.php
113
     *
114
     * @return bool
115
     */
116 26
    public function valid()
117
    {
118 26
        return ($this->filePointer && !$this->hasReachedEndOfFile);
119
    }
120
121
    /**
122
     * Move forward to next element. Reads data for the next unprocessed row.
123
     * @see http://php.net/manual/en/iterator.next.php
124
     *
125
     * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8
126
     * @return void
127
     */
128 26
    public function next()
129
    {
130 26
        $this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
131
132 26
        if (!$this->hasReachedEndOfFile) {
133 26
            $this->readDataForNextRow();
134
        }
135 26
    }
136
137
    /**
138
     * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8
139
     * @return void
140
     */
141 26
    protected function readDataForNextRow()
142
    {
143
        do {
144 26
            $rowData = $this->getNextUTF8EncodedRow();
145 26
        } while ($this->shouldReadNextRow($rowData));
146
147 26
        if ($rowData !== false) {
148
            // str_replace will replace NULL values by empty strings
149 24
            $rowDataBufferAsArray = str_replace(null, null, $rowData);
150 24
            $this->rowDataBuffer = $this->entityFactory->createRowFromArray($rowDataBufferAsArray);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->entityFactory->cr...($rowDataBufferAsArray) of type object<Box\Spout\Reader\Common\Entity\Row> is incompatible with the declared type array|null of property $rowDataBuffer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
151 24
            $this->numReadRows++;
152
        } else {
153
            // If we reach this point, it means end of file was reached.
154
            // This happens when the last lines are empty lines.
155 9
            $this->hasReachedEndOfFile = true;
156
        }
157 26
    }
158
159
    /**
160
     * @param array|bool $currentRowData
161
     * @return bool Whether the data for the current row can be returned or if we need to keep reading
162
     */
163 26
    protected function shouldReadNextRow($currentRowData)
164
    {
165 26
        $hasSuccessfullyFetchedRowData = ($currentRowData !== false);
166 26
        $hasNowReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
167 26
        $isEmptyLine = $this->isEmptyLine($currentRowData);
168
169
        return (
170 26
            (!$hasSuccessfullyFetchedRowData && !$hasNowReachedEndOfFile) ||
171 26
            (!$this->shouldPreserveEmptyRows && $isEmptyLine)
172
        );
173
    }
174
175
    /**
176
     * Returns the next row, converted if necessary to UTF-8.
177
     * As fgetcsv() does not manage correctly encoding for non UTF-8 data,
178
     * we remove manually whitespace with ltrim or rtrim (depending on the order of the bytes)
179
     *
180
     * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8
181
     * @return array|false The row for the current file pointer, encoded in UTF-8 or FALSE if nothing to read
182
     */
183 26
    protected function getNextUTF8EncodedRow()
184
    {
185 26
        $encodedRowData = $this->globalFunctionsHelper->fgetcsv($this->filePointer, self::MAX_READ_BYTES_PER_LINE, $this->fieldDelimiter, $this->fieldEnclosure);
186 26
        if ($encodedRowData === false) {
187 9
            return false;
188
        }
189
190 25
        foreach ($encodedRowData as $cellIndex => $cellValue) {
191 25
            switch ($this->encoding) {
192 25
                case EncodingHelper::ENCODING_UTF16_LE:
193 22
                case EncodingHelper::ENCODING_UTF32_LE:
194
                    // remove whitespace from the beginning of a string as fgetcsv() add extra whitespace when it try to explode non UTF-8 data
195 4
                    $cellValue = ltrim($cellValue);
196 4
                    break;
197
198 21
                case EncodingHelper::ENCODING_UTF16_BE:
199 20
                case EncodingHelper::ENCODING_UTF32_BE:
200
                    // remove whitespace from the end of a string as fgetcsv() add extra whitespace when it try to explode non UTF-8 data
201 2
                    $cellValue = rtrim($cellValue);
202 2
                    break;
203
            }
204
205 25
            $encodedRowData[$cellIndex] = $this->encodingHelper->attemptConversionToUTF8($cellValue, $this->encoding);
206
        }
207
208 25
        return $encodedRowData;
209
    }
210
211
    /**
212
     * @param array|bool $lineData Array containing the cells value for the line
213
     * @return bool Whether the given line is empty
214
     */
215 26
    protected function isEmptyLine($lineData)
216
    {
217 26
        return (is_array($lineData) && count($lineData) === 1 && $lineData[0] === null);
218
    }
219
220
    /**
221
     * Return the current element from the buffer
222
     * @see http://php.net/manual/en/iterator.current.php
223
     *
224
     * @return array|null
225
     */
226 24
    public function current()
227
    {
228 24
        return $this->rowDataBuffer;
229
    }
230
231
    /**
232
     * Return the key of the current element
233
     * @see http://php.net/manual/en/iterator.key.php
234
     *
235
     * @return int
236
     */
237 18
    public function key()
238
    {
239 18
        return $this->numReadRows;
240
    }
241
242
    /**
243
     * Cleans up what was created to iterate over the object.
244
     *
245
     * @return void
246
     */
247
    public function end()
248
    {
249
        // do nothing
250
    }
251
}
252