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 = fgetcsv( |
130
|
|
|
$this->getFileHandle(), |
131
|
|
|
null, |
132
|
|
|
$this->getDelimiter(), |
133
|
|
|
$this->getEnclosure(), |
134
|
|
|
$this->getEscapeCharacter() |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
if (!is_array($cells)) { |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$row = new Row($cells, $this); |
142
|
|
|
|
143
|
|
|
return $row; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritDoc |
148
|
|
|
*/ |
149
|
|
|
public function current() |
150
|
|
|
{ |
151
|
|
|
return $this->row; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritDoc |
156
|
|
|
*/ |
157
|
|
|
public function next() |
158
|
|
|
{ |
159
|
|
|
++$this->index; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
|
|
public function key() |
166
|
|
|
{ |
167
|
|
|
return $this->index; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritDoc |
172
|
|
|
*/ |
173
|
|
|
public function valid() |
174
|
|
|
{ |
175
|
|
|
$this->row = $this->getNextRow(); |
176
|
|
|
|
177
|
|
|
return $this->row instanceof Row; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritDoc |
182
|
|
|
*/ |
183
|
|
|
public function rewind() |
184
|
|
|
{ |
185
|
|
|
if ($this->getFileHandle()) { |
186
|
|
|
rewind($this->getFileHandle()); |
187
|
|
|
} else { |
188
|
|
|
$this->createFileHandle(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->index = 0; |
192
|
|
|
|
193
|
|
|
if ($this->hasHeaders()) { |
194
|
|
|
$this->headers = $this->getNextRow(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @inheritDoc |
200
|
|
|
*/ |
201
|
|
|
public function count() |
202
|
|
|
{ |
203
|
|
|
return count(iterator_to_array($this)); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|