RowIterator::rewind()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Akeneo\Component\SpreadsheetParser\Csv;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
/**
8
 * Row iterator for CSV
9
 *
10
 * The following options are available :
11
 *  - length:    the maximum length of read lines
12
 *  - delimiter: the CSV delimiter character
13
 *  - enclosure: the CSV enclosure character
14
 *  - escape:    the CSV escape character
15
 *  - encoding:  the encoding of the CSV file
16
 *
17
 * @author    Antoine Guigan <[email protected]>
18
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
19
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
20
 */
21
class RowIterator implements \Iterator
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * @var array
30
     */
31
    protected $options;
32
33
    /**
34
     * @var resource
35
     */
36
    protected $fileHandle;
37
38
    /**
39
     * @var int
40
     */
41
    protected $currentKey;
42
43
    /**
44
     * @var array
45
     */
46
    protected $currentValue;
47
48
    /**
49
     * @var boolean
50
     */
51
    protected $valid;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param string $path
57
     * @param array  $options
58
     */
59
    public function __construct(
60
        $path,
61
        array $options
62
    ) {
63
        $this->path = $path;
64
        $resolver = new OptionsResolver;
65
        $this->setDefaultOptions($resolver);
66
        $this->options = $resolver->resolve($options);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function current()
73
    {
74
        return $this->currentValue;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function key()
81
    {
82
        return $this->currentKey;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function next()
89
    {
90
        $this->currentValue = fgetcsv(
91
            $this->fileHandle,
92
            $this->options['length'],
93
            $this->options['delimiter'],
94
            $this->options['enclosure'],
95
            $this->options['escape']
96
        );
97
        $this->currentKey++;
98
        $this->valid = (false !== $this->currentValue);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function rewind()
105
    {
106
        if ($this->fileHandle) {
107
            rewind($this->fileHandle);
108
        } else {
109
            $this->openResource();
110
        }
111
        $this->currentKey = 0;
112
        $this->next();
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function valid()
119
    {
120
        return $this->valid;
121
    }
122
123
    /**
124
     * Sets the default options
125
     *
126
     * @param OptionsResolver $resolver
127
     */
128
    protected function setDefaultOptions(OptionsResolver $resolver)
129
    {
130
        $resolver->setDefined(['encoding']);
131
        $resolver->setDefaults(
132
            [
133
                'length'    => null,
134
                'delimiter' => ',',
135
                'enclosure' => '"',
136
                'escape'    => '\\'
137
            ]
138
        );
139
    }
140
141
    /**
142
     * Opens the file resource
143
     */
144
    protected function openResource()
145
    {
146
        $this->fileHandle = fopen($this->path, 'r');
147
        $currentEncoding = $this->getCurrentEncoding();
148
149
        if (isset($this->options['encoding']) && $currentEncoding !== $this->options['encoding']) {
150
            stream_filter_prepend(
151
                $this->fileHandle,
152
                sprintf(
153
                    "convert.iconv.%s/%s",
154
                    $this->options['encoding'],
155
                    $this->getCurrentEncoding()
156
                )
157
            );
158
        }
159
    }
160
161
    /**
162
     * Returns the server encoding
163
     *
164
     * @return string
165
     */
166
    protected function getCurrentEncoding()
167
    {
168
        $locale = explode('.', setlocale(LC_CTYPE, 0));
169
170
        return isset($locale[1]) ? $locale[1] : 'UTF8';
171
    }
172
}
173