Completed
Push — master ( bb5748...f73a2b )
by Andy
03:16
created

Reader   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 15.38%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
lcom 2
cbo 2
dl 0
loc 187
ccs 8
cts 52
cp 0.1538
rs 10
c 3
b 0
f 0

15 Methods

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