Passed
Push — master ( 75715f...53a757 )
by Andy
05:10
created

Reader::createDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 4
cp 0.5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1.125
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 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 1
    public function createDocument()
42
    {
43 1
        parent::createDocument();
44
45
        $this->getDocument()->setCsvControl($this->getDelimiter(), $this->getEnclosure(), $this->getEscapeCharacter());
46
    }
47
48
    /**
49
     * @return Row
50
     */
51
    public function getHeaders()
52
    {
53
        return $this->headers;
54
    }
55
56 3
    public function getHeader($key)
57
    {
58 3
        if (!isset($this->headers[$key])) {
59 3
            return $key;
60
        }
61
62
        return $this->headers[$key];
63
    }
64
65
    /**
66
     * @param mixed              $key
67
     * @param FormatterInterface $formatter Formatter instance.
68
     *
69
     * @return $this
70
     */
71
    public function addFormatter($key, FormatterInterface $formatter)
72
    {
73
        $this->formatters[$key] = $formatter;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param array|\Traversable $formatters
80
     *
81
     * @return $this
82
     */
83
    public function addFormatters($formatters)
84
    {
85
        foreach ($formatters as $key => $formatter) {
86
            $this->addFormatter($key, $formatter);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param mixed $key
94
     *
95
     * @return FormatterInterface
96
     */
97 3
    public function getFormatter($key)
98
    {
99 3
        if (!isset($this->formatters[$key])) {
100 3
            $class = static::$defaultFormatter;
101
102 3
            $this->formatters[$key] = new $class();
103
        }
104
105 3
        return $this->formatters[$key];
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getEscapeCharacter()
112
    {
113
        return $this->escapeCharacter;
114
    }
115
116
    /**
117
     * @param string $escapeCharacter
118
     *
119
     * @return AbstractCsv
120
     */
121
    public function setEscapeCharacter($escapeCharacter)
122
    {
123
        $this->escapeCharacter = $escapeCharacter;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Reads the next line in the CSV file
130
     * and returns a Row object from it.
131
     *
132
     * @return Row
133
     */
134
    protected function getNextRow()
135
    {
136
        $cells = $this->getDocument()->current();
137
138
        if (!is_array($cells)) {
139
            return null;
140
        }
141
142
        $row = new Row($cells, $this);
143
144
        return $row;
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function current()
151
    {
152
        return $this->row;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function next()
159
    {
160
        $this->getDocument()->next();
161
162
        ++$this->index;
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function key()
169
    {
170
        return $this->index;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function valid()
177
    {
178
        $this->row = $this->getNextRow();
179
180
        return $this->row instanceof Row;
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function rewind()
187
    {
188
        $this->getDocument()->rewind();
189
190
        $this->index = 0;
191
192
        if ($this->hasHeaders()) {
193
            $this->headers = $this->getNextRow();
194
        }
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200
    public function count()
201
    {
202
        return count(iterator_to_array($this));
203
    }
204
}
205