CsvReader::doCurrent()   A
last analyzed

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 TreeHouse\Feeder\Reader;
4
5
use SplFileObject;
6
use Symfony\Component\HttpFoundation\ParameterBag;
7
use TreeHouse\Feeder\Resource\ResourceInterface;
8
9
class CsvReader extends AbstractReader
10
{
11
    /**
12
     * @var SplFileObject
13
     */
14
    protected $fileObject;
15
16
    /**
17
     * @var array
18
     */
19
    protected $mapping;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $useFirstRow;
25
26
    /**
27
     * @var string
28
     */
29
    protected $delimiter = ',';
30
31
    /**
32
     * @var string
33
     */
34
    protected $enclosure = '"';
35
36
    /**
37
     * @var string
38
     */
39
    protected $escape = '\\';
40
41
    /**
42
     * @var bool
43
     */
44
    protected $convertNull;
45
46
    /**
47
     * Sets a mapping to use for each row. If the CSV has the column names exported,
48
     * you can use {@link useFirstRow} to auto-generate the field mapping.
49
     *
50
     * @param array $mapping
51
     */
52 4
    public function setFieldMapping(array $mapping)
53
    {
54 4
        $this->mapping = $mapping;
55 4
    }
56
57
    /**
58
     * When true, the first row in the CSV file is used to generate the field mapping.
59
     *
60
     * @param bool $bool
61
     */
62 2
    public function useFirstRow($bool = true)
63
    {
64 2
        $this->useFirstRow = (boolean) $bool;
65 2
    }
66
67
    /**
68
     * @param string $delimiter
69
     */
70 2
    public function setDelimiter($delimiter = ',')
71
    {
72 2
        $this->delimiter = $delimiter;
73 2
    }
74
75
    /**
76
     * @param string $enclosure
77
     */
78 2
    public function setEnclosure($enclosure = '"')
79
    {
80 2
        $this->enclosure = $enclosure;
81 2
    }
82
83
    /**
84
     * @param string $escape
85
     */
86 2
    public function setEscape($escape = '\\')
87
    {
88 2
        $this->escape = $escape;
89 2
    }
90
91
    /**
92
     * @return int
93
     */
94
    public function getRowNumber()
95
    {
96
        return $this->key() + 1;
97
    }
98
99
    /**
100
     * When true, "null" and "NULL" are converted to NULL.
101
     *
102
     * @param bool $bool
103
     */
104 2
    public function convertNull($bool = true)
105
    {
106 2
        $this->convertNull = (boolean) $bool;
107 2
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112 16
    protected function serialize($data)
113
    {
114
        // convert data keys if a mapping is given
115 16
        if ($this->mapping === null) {
116 12
            $this->mapping = array_combine(array_keys($data), array_keys($data));
117 12
        }
118
119 16
        $item = new ParameterBag();
120 16
        foreach ($this->mapping as $index => $field) {
121 16
            $value = array_key_exists($index, $data) ? $data[$index] : null;
122
123 16
            if ($this->convertNull && in_array($value, ['null', 'NULL'])) {
124 2
                $value = null;
125 2
            }
126
127 16
            $item->set($field, $value);
128 16
        }
129
130 16
        return $item;
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    protected function doKey()
137
    {
138
        return $this->fileObject->key();
139
    }
140
141
    /**
142
     * @inheritdoc
143
     */
144 16
    protected function doCurrent()
145
    {
146 16
        return $this->fileObject->current();
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152 16
    protected function doNext()
153
    {
154 16
        $this->fileObject->next();
155 16
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160 16
    protected function doValid()
161
    {
162 16
        return $this->fileObject->valid() && is_array($this->fileObject->current());
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168
    protected function doRewind()
169
    {
170
        $this->fileObject->rewind();
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176 16
    protected function createReader(ResourceInterface $resource)
177
    {
178 16
        $this->fileObject = new SplFileObject($resource->getFile()->getPathname());
179 16
        $this->fileObject->setFlags(SplFileObject::READ_CSV | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
180 16
        $this->fileObject->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
181
182 16
        if ($this->useFirstRow) {
183
            /** @var array $mapping */
184 2
            $mapping = $this->fileObject->current();
185
186 2
            $this->setFieldMapping($mapping);
187 2
            $this->fileObject->next();
188 2
        }
189 16
    }
190
}
191