29 protected $delimiter =
null;
32 protected $enclosure =
null;
35 protected $response = [];
38 protected $line_num = 0;
41 protected $SplFileObj =
null;
47 protected $array_template = [];
57 public function __construct($csv_path =
'', $delimiter =
',', $enclosure =
null) {
61 ->Set(
'delimiter', $delimiter)
62 ->Set(
'enclosure', $enclosure)
63 ->SetFileObject($csv_path)
74 $this->SplFileObj->seek($this->line_num);
76 if ($this->SplFileObj->eof()) {
82 ->CombineCsvArray($this->SplFileObj->current())
93 return ! is_null($this->SplFileObj);
117 protected function Set($prop, $value) {
119 if (! $value || empty($value)) {
123 $this->{$prop} = $value;
136 $this->SplFileObj =
new SplFileObject($file);
147 $this->SplFileObj->seek(0);
148 $this->array_template = str_getcsv($this->SplFileObj->current(), $this->delimiter, $this->enclosure);
164 $data = str_getcsv($line, $this->delimiter, $this->enclosure);
168 $this->response[
'errors'][] =
" 169 The data length is incorrect, please check the CSV file. 170 The columns in the header MUST match the columns in the data rows." . PHP_EOL;
186 return count($data) == count($this->array_template);
196 return array_combine($this->array_template, $data);
206 if ($this->SplFileObj) {
207 $this->SplFileObj =
null;
219 return $this->response;
228 return count($this->response[
'errors']) > 0;
DataMatchesTemplateLength($data)
CombineCsvArray($line='')
CreateAssociativeArrays($data)
__construct($csv_path='', $delimiter=',', $enclosure=null)