1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Csv; |
4
|
|
|
|
5
|
|
|
use Palmtree\Csv\Normalizer\NormalizerInterface; |
6
|
|
|
use Palmtree\Csv\Normalizer\NullNormalizer; |
7
|
|
|
use Palmtree\Csv\Row\Row; |
8
|
|
|
use Palmtree\Csv\Util\StringUtil; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Reads a CSV file by loading each line into memory |
12
|
|
|
* one at a time. |
13
|
|
|
*/ |
14
|
|
|
class Reader extends AbstractCsv implements \Iterator |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $defaultNormalizer = NullNormalizer::class; |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $openMode = 'r'; |
20
|
|
|
/** @var NormalizerInterface[] */ |
21
|
|
|
protected $normalizers = []; |
22
|
|
|
/** @var Row */ |
23
|
|
|
protected $headers; |
24
|
|
|
/** @var Row */ |
25
|
|
|
protected $row; |
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $bom = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $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
|
2 |
|
public function getHeaders() |
45
|
|
|
{ |
46
|
2 |
|
if (is_null($this->headers) && $this->hasHeaders()) { |
47
|
2 |
|
$this->rewind(); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return $this->headers; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $key |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
6 |
|
public function getHeader($key) |
59
|
|
|
{ |
60
|
6 |
|
if (!isset($this->headers[$key])) { |
61
|
6 |
|
return $key; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $this->headers[$key]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $key |
69
|
|
|
* @param NormalizerInterface $normalizer Normalizer instance. |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function addNormalizer($key, NormalizerInterface $normalizer) |
74
|
|
|
{ |
75
|
|
|
$this->normalizers[$key] = $normalizer; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array|\Traversable $normalizers |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function addNormalizers($normalizers) |
86
|
|
|
{ |
87
|
|
|
foreach ($normalizers as $key => $normalizer) { |
88
|
|
|
$this->addNormalizer($key, $normalizer); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param mixed $key |
96
|
|
|
* |
97
|
|
|
* @return NormalizerInterface |
98
|
|
|
*/ |
99
|
6 |
|
public function getNormalizer($key) |
100
|
|
|
{ |
101
|
6 |
|
if (!isset($this->normalizers[$key])) { |
102
|
6 |
|
$class = $this->getDefaultNormalizer(); |
103
|
|
|
|
104
|
6 |
|
$this->normalizers[$key] = new $class(); |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
return $this->normalizers[$key]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Reads the next line in the CSV file |
112
|
|
|
* and returns a Row object from it. |
113
|
|
|
* |
114
|
|
|
* @return Row|null |
115
|
|
|
*/ |
116
|
3 |
|
protected function getCurrentRow() |
117
|
|
|
{ |
118
|
3 |
|
$cells = $this->getDocument()->current(); |
119
|
|
|
|
120
|
3 |
|
if (!is_array($cells)) { |
121
|
1 |
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
if ($this->key() === 0 && $this->hasBom()) { |
125
|
1 |
|
$stripped = StringUtil::stripBom($cells[0], StringUtil::BOM_UTF8); |
126
|
|
|
|
127
|
1 |
|
if ($stripped !== $cells[0]) { |
128
|
1 |
|
$cells[0] = trim($stripped, $this->getEnclosure()); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
$row = new Row($cells, $this); |
133
|
|
|
|
134
|
3 |
|
return $row; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritDoc |
139
|
|
|
*/ |
140
|
1 |
|
public function current() |
141
|
|
|
{ |
142
|
1 |
|
return $this->row; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritDoc |
147
|
|
|
*/ |
148
|
3 |
|
public function next() |
149
|
|
|
{ |
150
|
3 |
|
$this->getDocument()->next(); |
151
|
3 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritDoc |
155
|
|
|
*/ |
156
|
3 |
|
public function key() |
157
|
|
|
{ |
158
|
3 |
|
return $this->getDocument()->key(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritDoc |
163
|
|
|
*/ |
164
|
1 |
|
public function valid() |
165
|
|
|
{ |
166
|
1 |
|
$this->row = $this->getCurrentRow(); |
167
|
|
|
|
168
|
1 |
|
return $this->row instanceof Row; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritDoc |
173
|
|
|
*/ |
174
|
3 |
|
public function rewind() |
175
|
|
|
{ |
176
|
3 |
|
$this->getDocument()->rewind(); |
177
|
|
|
|
178
|
3 |
|
if ($this->hasHeaders()) { |
179
|
|
|
// Set headers to null first so the header row is a zero-based array and can be used |
180
|
|
|
// to set the array keys of all other rows. |
181
|
3 |
|
$this->headers = null; |
182
|
3 |
|
$this->headers = $this->getCurrentRow(); |
183
|
3 |
|
$this->next(); |
184
|
|
|
} |
185
|
3 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $defaultNormalizer |
189
|
|
|
* |
190
|
|
|
* @return Reader |
191
|
|
|
*/ |
192
|
|
|
public function setDefaultNormalizer($defaultNormalizer) |
193
|
|
|
{ |
194
|
|
|
$this->defaultNormalizer = $defaultNormalizer; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
6 |
|
public function getDefaultNormalizer() |
203
|
|
|
{ |
204
|
6 |
|
return $this->defaultNormalizer; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param bool $bom |
209
|
|
|
* |
210
|
|
|
* @return Reader |
211
|
|
|
*/ |
212
|
2 |
|
public function setBom($bom) |
213
|
|
|
{ |
214
|
2 |
|
$this->bom = $bom; |
215
|
|
|
|
216
|
2 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
3 |
|
public function hasBom() |
223
|
|
|
{ |
224
|
3 |
|
return $this->bom; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
public function toArray() |
231
|
|
|
{ |
232
|
|
|
$result = []; |
233
|
|
|
foreach ($this as $rowKey => $row) { |
234
|
|
|
$result[$rowKey] = $row->toArray(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $result; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|