Skip to content

Commit 53405cc

Browse files
CSVParser
1 parent 25a9d5b commit 53405cc

1 file changed

Lines changed: 103 additions & 127 deletions

File tree

src/CSV.php

Lines changed: 103 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ class CSV {
3030
*/
3131
function parse($data) {
3232
$rawData = NULL;
33+
$dataType = NULL;
3334
//Check data type
3435
if(is_array($data)) {
3536
$rawData = $data;
37+
$dataType = 'array';
3638
} else if(is_object($data)) {
3739
$rawData = (array) $data;
40+
$dataType = 'array';
3841
} else if($this->is_json($data)) {
3942
$rawData = json_decode($data, true);
43+
$dataType = 'array';
4044
} else if(is_string($data)) {
4145
if(is_file($data)) {
4246
if(is_readable($data)) {
@@ -47,6 +51,7 @@ function parse($data) {
4751
$tmpData[] = array_map('trim', explode($this->delimiter, $row));
4852
}
4953
$rawData = $tmpData;
54+
$dataType = 'csv';
5055
} else {
5156
//Throw error
5257
throw new Exception('Error: Can not read file, permission denied');
@@ -59,73 +64,58 @@ function parse($data) {
5964
//Throw error
6065
throw new Exception('Error: CSVParser invalid data type');
6166
}
62-
//Parse data
63-
if(!empty($rawData)) {
64-
if($this->ignoreHeader == true) {
65-
if(!empty($this->header)) {
66-
//Parse body data
67-
$parseData = [];
68-
foreach($rawData as $row) {
69-
$tmpData = array();
70-
foreach($row as $key => $value) {
71-
if(isset($this->header[$key])) {
72-
$tmpData[$this->header[$key]] = $value;
73-
} else {
74-
$tmpData[count($tmpData)] = $value;
75-
}
76-
}
77-
if(!empty($tmpData)) {
78-
$parseData[] = $tmpData;
79-
}
67+
68+
//Parse header
69+
if($dataType == 'array') {
70+
if($this->ignoreHeader == false) {
71+
//Parse user custom header
72+
if(empty($this->header) && !empty($rawData)) {
73+
if(isset($rawData[$this->headerOffset])) {
74+
$this->header = array_keys($rawData[$this->headerOffset]);
75+
} else {
76+
throw new Exception('Error : header not found at offset '.$this->headerOffset);
8077
}
81-
$this->parsedData = $parseData;
82-
} else {
83-
$this->parsedData = $rawData;
8478
}
85-
} else {
86-
//Parse header
87-
if(empty($this->header)) {
88-
if($this->headerOffset == 0) {
89-
$this->header = $rawData[0];
90-
//Remove header from data
91-
array_shift($rawData);
92-
} else if(isset($rawData[$this->headerOffset])) {
93-
$this->header = $rawData[$this->headerOffset];
94-
//Remove header from data
79+
}
80+
} else {
81+
if($this->ignoreHeader == false) {
82+
//Parse user custom header
83+
if(!empty($this->header)) {
84+
//Ignore header
85+
if(isset($rawData[$this->headerOffset])) {
9586
unset($rawData[$this->headerOffset]);
96-
} else {
97-
//Throw error header not found
98-
throw new Exception('Error: Header not found');
9987
}
10088
} else {
101-
if($this->headerOffset == 0) {
102-
//Remove header from data
103-
array_shift($rawData);
104-
} else if(isset($rawData[$this->headerOffset])) {
105-
//Remove header from data
89+
if(isset($rawData[$this->headerOffset])) {
90+
$this->header = $rawData[$this->headerOffset];
10691
unset($rawData[$this->headerOffset]);
10792
} else {
108-
//Throw error header not found
109-
throw new Exception('Error: Header not found');
93+
throw new Exception('Error : header not found at offset '.$this->headerOffset);
11094
}
11195
}
112-
//Parse body data
113-
$parseData = [];
114-
foreach($rawData as $row) {
115-
$tmpData = array();
116-
foreach($row as $key => $value) {
117-
if(isset($this->header[$key])) {
118-
$tmpData[$this->header[$key]] = $value;
119-
} else {
120-
$tmpData[count($tmpData)] = $value;
121-
}
122-
}
123-
if(!empty($tmpData)) {
124-
$parseData[] = $tmpData;
96+
}
97+
}
98+
99+
//Parse data
100+
if(!empty($rawData)) {
101+
//Parse body data
102+
$parseData = [];
103+
foreach($rawData as $row) {
104+
$tmpData = array();
105+
$i = 0;
106+
foreach($row as $key => $value) {
107+
if(isset($this->header[$i])) {
108+
$tmpData[$this->header[$i]] = $value;
109+
} else {
110+
$tmpData[count($tmpData)] = $value;
125111
}
112+
$i++;
113+
}
114+
if(!empty($tmpData)) {
115+
$parseData[] = $tmpData;
126116
}
127-
$this->parsedData = $parseData;
128117
}
118+
$this->parsedData = $parseData;
129119
}
130120
}
131121

@@ -223,66 +213,74 @@ function limit(...$limit) {
223213
* @return array
224214
*/
225215
function toArray(array $header=NULL) : array {
226-
$parseHeader = [];
227-
$parseData = [];
216+
$parsedHeader = [];
217+
$parsedData = [];
218+
//Set data limit
219+
if(!empty($this->limit)) {
220+
if(isset($this->limit['start']) && isset($this->limit['end'])) {
221+
$tmpParsedData = array_slice($this->parsedData, $this->limit['start'], $this->limit['end']);
222+
} else if(isset($this->limit['start'])) {
223+
$tmpParsedData = array_slice($this->parsedData, $this->limit['start']);
224+
}
225+
} else {
226+
$tmpParsedData = $this->parsedData;
227+
}
228+
229+
//Check header is valid or not
228230
if(!empty($header)) {
229-
//Check header is valid or not
230-
if(!empty($this->header)) {
231-
if($this->ignoreHeaderCase == true) {
232-
$ignoreHeaderCase = array_map('strtolower', $this->header);
233-
$tmpHeader = array_combine($ignoreHeaderCase, $this->header);
234-
} else {
235-
$tmpHeader = array_combine($this->header, $this->header);
236-
}
237-
} else if(!empty($this->parsedData)) {
238-
$tmpHeader = array_combine(array_keys($this->parsedData[0]), array_keys($this->parsedData[0]));
231+
if(!empty($this->header) && $this->ignoreHeaderCase == true) {
232+
$ignoreHeaderCase = array_map('strtolower', $this->header);
233+
$tmpHeader = array_combine($ignoreHeaderCase, $this->header);
234+
} else {
235+
$tmpHeader = array_combine($this->header, $this->header);
239236
}
240237
//Parse header data
241-
foreach($header as $col) {
242-
$tmpCol = $col;
243-
if($this->ignoreHeaderCase == true) {
244-
$col = strtolower($col);
245-
}
246-
if(!array_key_exists($col, $tmpHeader)) {
247-
//Throw error header not found
248-
throw new Exception("Error: '".$tmpCol."' header not found");
249-
} else {
250-
$parseHeader[$tmpCol] = $tmpHeader[$col];
251-
}
252-
}
253-
//Parse body data
254-
if(!empty($this->limit)) {
255-
if(isset($this->limit['start']) && isset($this->limit['end'])) {
256-
$tmpParsedData = array_slice($this->parsedData, $this->limit['start'], $this->limit['end']);
257-
} else if(isset($this->limit['start'])) {
258-
$tmpParsedData = array_slice($this->parsedData, $this->limit['start']);
238+
if(!empty($tmpHeader)) {
239+
foreach($header as $col) {
240+
$tmpCol = $col;
241+
if($this->ignoreHeaderCase == true) {
242+
$col = strtolower($col);
243+
}
244+
//Check header exists or not
245+
if(!array_key_exists($col, $tmpHeader)) {
246+
//Throw error header not found
247+
throw new Exception("Error: '".$tmpCol."' header not found");
248+
} else {
249+
$parsedHeader[$tmpCol] = $tmpHeader[$col];
250+
}
259251
}
260252
} else {
261-
$tmpParsedData = $this->parsedData;
253+
//Check header index exists or not
254+
foreach($header as $col) {
255+
if(is_int($col) && isset($tmpParsedData[$this->headerOffset])) {
256+
$col = $col == 0 ? $col : $col - 1;
257+
if(!array_key_exists($col, $tmpParsedData[$this->headerOffset])) {
258+
//Throw error header not found
259+
throw new Exception("Error: '".$col."' header not found");
260+
} else {
261+
$parsedHeader[$col] = $col;
262+
}
263+
} else {
264+
//Throw error header not found
265+
throw new Exception("Error: '".$col."' header not found");
266+
}
267+
}
262268
}
269+
270+
//Parse data
263271
foreach($tmpParsedData as $row) {
264272
$tmpRow = [];
265-
foreach($parseHeader as $key => $val) {
273+
foreach($parsedHeader as $key => $val) {
266274
$tmpRow[$key] = $row[$val];
267275
}
268276
if(!empty($tmpRow)) {
269-
$parseData[] = $tmpRow;
277+
$parsedData[] = $tmpRow;
270278
}
271279
}
272280
} else {
273-
//Parse body data
274-
if(!empty($this->limit)) {
275-
if(isset($this->limit['start']) && isset($this->limit['end'])) {
276-
$tmpParsedData = array_slice($this->parsedData, $this->limit['start'], $this->limit['end']);
277-
} else if(isset($this->limit['start'])) {
278-
$tmpParsedData = array_slice($this->parsedData, $this->limit['start']);
279-
}
280-
$parseData = $tmpParsedData;
281-
} else {
282-
$parseData = $this->parsedData;
283-
}
281+
$parsedData = $tmpParsedData;
284282
}
285-
return $parseData;
283+
return $parsedData;
286284
}
287285

288286
/**
@@ -316,34 +314,12 @@ function toCsv(array $header=NULL) : string {
316314
$parsedData = $this->toArray($header);
317315
$csvData = '';
318316
if(!empty($parsedData)) {
319-
if(!empty($header)) {
320-
//Check header is valid or not
321-
if(!empty($this->header)) {
322-
if($this->ignoreHeaderCase == true) {
323-
$ignoreHeaderCase = array_map('strtolower', $this->header);
324-
$tmpHeader = array_combine($ignoreHeaderCase, $this->header);
325-
} else {
326-
$tmpHeader = array_combine($this->header, $this->header);
327-
}
328-
}
329-
//Parse header data
330-
foreach($header as $col) {
331-
$tmpCol = $col;
332-
if($this->ignoreHeaderCase == true) {
333-
$col = strtolower($col);
334-
}
335-
if(!array_key_exists($col, $tmpHeader)) {
336-
//Throw error header not found
337-
throw new Exception("Error: '".$tmpCol."' header not found");
338-
} else {
339-
$parseHeader[$tmpCol] = $tmpHeader[$col];
340-
}
317+
if(!empty($this->header)) {
318+
if(!empty($header)) {
319+
$csvData .= implode($this->delimiter, $header).PHP_EOL;
320+
} else {
321+
$csvData .= implode($this->delimiter, $this->header).PHP_EOL;
341322
}
342-
} else {
343-
$parseHeader = array_map('trim', $this->header);
344-
}
345-
if(!empty($parseHeader)) {
346-
$csvData .= implode($this->delimiter, $parseHeader).PHP_EOL;
347323
}
348324
foreach($parsedData as $row) {
349325
$csvData .= implode($this->delimiter, $row).PHP_EOL;

0 commit comments

Comments
 (0)