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