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