Passed
Push — master ( 665ba1...f79b8d )
by Andy
02:08
created

Reader::getCurrentRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Palmtree\Csv;
4
5
use Palmtree\Csv\Formatter\FormatterInterface;
6
use Palmtree\Csv\Formatter\StringFormatter;
7
use Palmtree\Csv\Row\Row;
8
9
/**
10
 * Reads a CSV file by loading each line into memory
11
 * one at a time.
12
 */
13
class Reader extends AbstractCsv implements \Iterator, \Countable
14
{
15
    public static $defaultFormatter = StringFormatter::class;
16
17
    protected $openMode = 'r';
18
    /** @var FormatterInterface[] */
19
    protected $formatters = [];
20
    /** @var Row */
21
    protected $headers;
22
    /** @var Row */
23
    protected $row;
24
    /** @var string */
25
    protected $escapeCharacter = "\0";
26
27
    /**
28
     * @param $file
29
     *
30
     * @return Reader
31
     */
32
    public static function read($file)
33
    {
34
        $csv = new static($file);
35
36
        return $csv;
37
    }
38
39 1
    public function createDocument()
40
    {
41 1
        parent::createDocument();
42
43
        $this->getDocument()->setCsvControl($this->getDelimiter(), $this->getEnclosure(), $this->getEscapeCharacter());
44
    }
45
46
    /**
47
     * @return Row
48
     */
49
    public function getHeaders()
50
    {
51
        return $this->headers;
52
    }
53
54 3
    public function getHeader($key)
55
    {
56 3
        if (!isset($this->headers[$key])) {
57 3
            return $key;
58
        }
59
60
        return $this->headers[$key];
61
    }
62
63
    /**
64
     * @param mixed              $key
65
     * @param FormatterInterface $formatter Formatter instance.
66
     *
67
     * @return $this
68
     */
69
    public function addFormatter($key, FormatterInterface $formatter)
70
    {
71
        $this->formatters[$key] = $formatter;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param array|\Traversable $formatters
78
     *
79
     * @return $this
80
     */
81
    public function addFormatters($formatters)
82
    {
83
        foreach ($formatters as $key => $formatter) {
84
            $this->addFormatter($key, $formatter);
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param mixed $key
92
     *
93
     * @return FormatterInterface
94
     */
95 3
    public function getFormatter($key)
96
    {
97 3
        if (!isset($this->formatters[$key])) {
98 3
            $class = static::$defaultFormatter;
99
100 3
            $this->formatters[$key] = new $class();
101
        }
102
103 3
        return $this->formatters[$key];
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getEscapeCharacter()
110
    {
111
        return $this->escapeCharacter;
112
    }
113
114
    /**
115
     * @param string $escapeCharacter
116
     *
117
     * @return AbstractCsv
118
     */
119
    public function setEscapeCharacter($escapeCharacter)
120
    {
121
        $this->escapeCharacter = $escapeCharacter;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Reads the next line in the CSV file
128
     * and returns a Row object from it.
129
     *
130
     * @return Row
131
     */
132
    protected function getCurrentRow()
133
    {
134
        $cells = $this->getDocument()->current();
135
136
        if (!is_array($cells)) {
137
            return null;
138
        }
139
140
        $row = new Row($cells, $this);
141
142
        return $row;
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function current()
149
    {
150
        return $this->row;
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function next()
157
    {
158
        $this->getDocument()->next();
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function key()
165
    {
166
        return $this->getDocument()->key();
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    public function valid()
173
    {
174
        $this->row = $this->getCurrentRow();
175
176
        return $this->row instanceof Row;
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function rewind()
183
    {
184
        $this->getDocument()->rewind();
185
186
        if ($this->hasHeaders()) {
187
            $this->headers = $this->getCurrentRow();
188
        }
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    public function count()
195
    {
196
        return count(iterator_to_array($this));
197
    }
198
}
199