1 | <?php |
||
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 array |
||
33 | */ |
||
34 | public static function read($file) |
||
35 | { |
||
36 | $csv = new static($file); |
||
37 | |||
38 | return iterator_to_array($csv); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return Row |
||
43 | */ |
||
44 | 3 | public function getHeaders() |
|
45 | { |
||
46 | 3 | return $this->headers; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param mixed $key |
||
51 | * @param FormatterInterface $formatter Formatter instance. |
||
52 | * |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function addFormatter($key, FormatterInterface $formatter) |
||
56 | { |
||
57 | $this->formatters[$key] = $formatter; |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param array|\Traversable $formatters |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function addFormatters($formatters) |
||
68 | { |
||
69 | foreach ($formatters as $key => $formatter) { |
||
70 | $this->addFormatter($key, $formatter); |
||
71 | } |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param mixed $key |
||
78 | * |
||
79 | * @return FormatterInterface |
||
80 | */ |
||
81 | 3 | public function getFormatter($key) |
|
82 | { |
||
83 | 3 | if (!isset($this->formatters[$key])) { |
|
84 | 3 | $class = static::$defaultFormatter; |
|
85 | |||
86 | 3 | $this->formatters[$key] = new $class(); |
|
87 | } |
||
88 | |||
89 | 3 | return $this->formatters[$key]; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getEscapeCharacter() |
||
96 | { |
||
97 | return $this->escapeCharacter; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $escapeCharacter |
||
102 | * |
||
103 | * @return AbstractCsv |
||
104 | */ |
||
105 | public function setEscapeCharacter($escapeCharacter) |
||
106 | { |
||
107 | $this->escapeCharacter = $escapeCharacter; |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Returns an array of cells for the next row. |
||
114 | * |
||
115 | * @return Row |
||
116 | */ |
||
117 | protected function getNextRow() |
||
118 | { |
||
119 | $cells = fgetcsv( |
||
120 | $this->getFileHandle(), |
||
121 | null, |
||
122 | $this->getDelimiter(), |
||
123 | $this->getEnclosure(), |
||
124 | $this->getEscapeCharacter() |
||
125 | ); |
||
126 | |||
127 | if (!is_array($cells)) { |
||
128 | return null; |
||
129 | } |
||
130 | |||
131 | $row = new Row($cells, $this); |
||
132 | |||
133 | return $row; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @inheritDoc |
||
138 | */ |
||
139 | public function current() |
||
140 | { |
||
141 | return $this->row; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @inheritDoc |
||
146 | */ |
||
147 | public function next() |
||
148 | { |
||
149 | ++$this->index; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | public function key() |
||
156 | { |
||
157 | return $this->index; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @inheritDoc |
||
162 | */ |
||
163 | public function valid() |
||
164 | { |
||
165 | $this->row = $this->getNextRow(); |
||
166 | |||
167 | return $this->row instanceof Row; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @inheritDoc |
||
172 | */ |
||
173 | public function rewind() |
||
174 | { |
||
175 | if ($this->getFileHandle()) { |
||
176 | rewind($this->getFileHandle()); |
||
177 | } else { |
||
178 | $this->createFileHandle(); |
||
179 | } |
||
180 | |||
181 | $this->index = 0; |
||
182 | |||
183 | if ($this->hasHeaders()) { |
||
184 | $this->headers = $this->getNextRow(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @inheritDoc |
||
190 | */ |
||
191 | public function count() |
||
195 | } |
||
196 |