1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 class CSVParser extends Object implements Iterator {
26 protected $filename;
27 protected $fileHandle;
28
29 30 31 32
33 protected $columnMap = array();
34
35 36 37 38
39 protected = null;
40
41 42 43
44 protected = null;
45
46 47 48
49 protected $currentRow = null;
50
51 52 53 54
55 protected $rowNum = 0;
56
57 58 59
60 protected $delimiter = ",";
61
62 63 64
65 protected $enclosure = '"';
66
67 68 69 70 71 72 73
74 function __construct($filename, $delimiter = ",", $enclosure = '"') {
75 $filename = Director::getAbsFile($filename);
76 $this->filename = $filename;
77 $this->delimiter = $delimiter;
78 $this->enclosure = $enclosure;
79 parent::__construct();
80 }
81
82 83 84 85 86 87 88 89 90 91 92
93 function mapColumns($columnMap) {
94 if($columnMap) {
95 $lowerColumnMap = array();
96 foreach($columnMap as $k => $v) {
97 $lowerColumnMap[strtolower($k)] = $v;
98 }
99 $this->columnMap = array_merge($this->columnMap, $lowerColumnMap);
100 }
101 }
102
103 104 105 106
107 function ($headerRow) {
108 $this->providedHeaderRow = $headerRow;
109 }
110
111 112 113
114 protected function openFile() {
115 ini_set('auto_detect_line_endings',1);
116 $this->fileHandle = fopen($this->filename,'r');
117
118
119 if($this->providedHeaderRow) {
120 $this->headerRow = $this->remapHeader($this->providedHeaderRow);
121 }
122 }
123
124 125 126
127 protected function closeFile() {
128 if($this->fileHandle) fclose($this->fileHandle);
129 $this->fileHandle = null;
130
131 $this->rowNum = 0;
132 $this->currentRow = null;
133 $this->headerRow = null;
134 }
135
136
137 138 139
140 protected function () {
141 $srcRow = fgetcsv($this->fileHandle, 0, $this->delimiter, $this->enclosure);
142 $this->headerRow = $this->remapHeader($srcRow);
143 }
144
145 146 147
148 protected function ($header) {
149 $mappedHeader = array();
150 foreach($header as $item) {
151 if(isset($this->columnMap[strtolower($item)])) $item = $this->columnMap[strtolower($item)];
152 $mappedHeader[] = $item;
153 }
154 return $mappedHeader;
155 }
156
157 158 159
160 protected function fetchCSVRow() {
161 if(!$this->fileHandle) $this->openFile();
162 if(!$this->headerRow) $this->fetchCSVHeader();
163
164 $this->rowNum++;
165
166 $srcRow = fgetcsv($this->fileHandle, 0, $this->delimiter, $this->enclosure);
167 if($srcRow) {
168 $row = array();
169 foreach($srcRow as $i => $value) {
170
171 $value = str_replace(
172 array('\\'.$this->enclosure,'\\'.$this->delimiter),
173 array($this->enclosure,$this->delimiter),$value);
174 if(array_key_exists($i, $this->headerRow)) {
175 if($this->headerRow[$i]) $row[$this->headerRow[$i]] = $value;
176 } else {
177 user_error("No heading for column $i on row $this->rowNum", E_USER_WARNING);
178 }
179 }
180
181 $this->currentRow = $row;
182 } else {
183 $this->closeFile();
184 }
185 return $this->currentRow;
186 }
187
188 189 190
191 function __destruct() {
192 $this->closeFile();
193 }
194
195
196
197 198 199
200 function rewind() {
201 $this->closeFile();
202 $this->fetchCSVRow();
203 }
204
205 206 207
208 function current() {
209 return $this->currentRow;
210 }
211
212 213 214
215 function key() {
216 return $this->rowNum;
217 }
218
219 220 221
222 function next() {
223 $this->fetchCSVRow();
224 return $this->currentRow;
225 }
226
227 228 229
230 function valid() {
231 return $this->currentRow ? true : false;
232 }
233
234
235 }
236
237 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-