Odyssey
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CsvToArray Class Reference

Public Member Functions

 __construct ($csv_path='', $delimiter=',', $enclosure=null)
 
 ParseData ()
 
 IsLinesToProcess ()
 
 CloseFile ()
 

Protected Member Functions

 InitResponseArray ()
 
 Set ($prop, $value)
 
 SetFileObject ($file)
 
 SetArrayTemplate ()
 
 CombineCsvArray ($line='')
 
 DataMatchesTemplateLength ($data)
 
 CreateAssociativeArrays ($data)
 
 ReturnResponse ()
 
 IsErrors ()
 

Protected Attributes

 $delimiter = null
 
 $enclosure = null
 
 $response = []
 
 $line_num = 0
 
 $SplFileObj = null
 
 $array_template = []
 

Detailed Description

A wrapper class for str_getcsv() to parse a CSV file into an array. Leaves off escape param, can be added if needed . . . . ASSUMES (and really should be) first row of CSV are field names. It is shifted off the stack and used as field names for associative array as below.

Usage: $obj = new CsvToArray([path to dummie data CSV file], [optional string delimiter], [optional string enclosure]); $obj->ParseData();

"Enclosure" is better known as "qualifier" as in quote-qualified fields.

Returns array [ 'errors' => [], 'data' => [ [int row index] => [ [string field] => [mixed value], .... ] ];

Definition at line 26 of file CsvToArray.php.

Constructor & Destructor Documentation

◆ __construct()

CsvToArray::__construct (   $csv_path = '',
  $delimiter = ',',
  $enclosure = null 
)

Constructor, initialize response and set path to CSV. Set delimiter and enclosure first, setArrayTemplate() uses them.

Parameters
string$csv_path
string$delimiter
string$enclosure
Returns
void

Definition at line 57 of file CsvToArray.php.

57  {
58 
59  $this
60  ->InitResponseArray()
61  ->Set('delimiter', $delimiter)
62  ->Set('enclosure', $enclosure)
63  ->SetFileObject($csv_path)
64  ->SetArrayTemplate();
65  }

Member Function Documentation

◆ CloseFile()

CsvToArray::CloseFile ( )

Close filehandles by setting SplFileObject to null. Public so if the requested number of records is less than file length we can close it.

Returns
$this

Definition at line 204 of file CsvToArray.php.

204  {
205 
206  if ($this->SplFileObj) {
207  $this->SplFileObj = null;
208  }
209 
210  return $this;
211  }

◆ CombineCsvArray()

CsvToArray::CombineCsvArray (   $line = '')
protected

Leverage str_getcsv()to parse the CSV file.

Parameters
string$line
Returns
$this

Definition at line 158 of file CsvToArray.php.

158  {
159 
160  if ($this->IsErrors()) {
161  return $this;
162  }
163 
164  $data = str_getcsv($line, $this->delimiter, $this->enclosure);
165 
166  if (! $this->DataMatchesTemplateLength($data)) {
167 
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;
171  return $this;
172  }
173 
174  $this->response['data'] = $this->CreateAssociativeArrays($data);
175 
176  return $this;
177  }
DataMatchesTemplateLength($data)
Definition: CsvToArray.php:184
CreateAssociativeArrays($data)
Definition: CsvToArray.php:194

◆ CreateAssociativeArrays()

CsvToArray::CreateAssociativeArrays (   $data)
protected

Map the data to the first row of fields so we have an associative array.

Parameters
array$data
Returns
array

Definition at line 194 of file CsvToArray.php.

194  {
195 
196  return array_combine($this->array_template, $data);
197  }

◆ DataMatchesTemplateLength()

CsvToArray::DataMatchesTemplateLength (   $data)
protected

Validate we can do array_combine

Parameters
array$data
Returns
bool

Definition at line 184 of file CsvToArray.php.

184  {
185 
186  return count($data) == count($this->array_template);
187  }

◆ InitResponseArray()

CsvToArray::InitResponseArray ( )
protected

Initialize the response

Returns
$this

Definition at line 100 of file CsvToArray.php.

100  {
101 
102  $this->response = [
103  'errors' => [],
104  'data' => []
105  ];
106 
107  return $this;
108  }

◆ IsErrors()

CsvToArray::IsErrors ( )
protected

Got errors?

Returns
bool

Definition at line 226 of file CsvToArray.php.

226  {
227 
228  return count($this->response['errors']) > 0;
229  }

◆ IsLinesToProcess()

CsvToArray::IsLinesToProcess ( )

Check that the SplFileObject is still an object (not null)

Returns
bool

Definition at line 91 of file CsvToArray.php.

91  {
92 
93  return ! is_null($this->SplFileObj);
94  }

◆ ParseData()

CsvToArray::ParseData ( )

Get the associative array of $this->array_template mapped to CSV data rows.

Returns
array|false

Definition at line 71 of file CsvToArray.php.

71  {
72 
73  $this->line_num++;
74  $this->SplFileObj->seek($this->line_num);
75 
76  if ($this->SplFileObj->eof()) {
77  $this->CloseFile();
78  return false;
79  }
80 
81  return $this
82  ->CombineCsvArray($this->SplFileObj->current())
83  ->ReturnResponse();
84  }

◆ ReturnResponse()

CsvToArray::ReturnResponse ( )
protected

Return a response, this approach allows chaining of methods above it.

Returns
array

Definition at line 217 of file CsvToArray.php.

217  {
218 
219  return $this->response;
220  }

◆ Set()

CsvToArray::Set (   $prop,
  $value 
)
protected

A typical setter, sets internal properties (delimiter, enclosure) Usually setters are public, don't think we'll need it externally

Parameters
string$propproperty identifier
string$valueproperty to set
Returns
$this

Definition at line 117 of file CsvToArray.php.

117  {
118 
119  if (! $value || empty($value)) {
120  return $this;
121  }
122 
123  $this->{$prop} = $value;
124 
125  return $this;
126  }

◆ SetArrayTemplate()

CsvToArray::SetArrayTemplate ( )
protected

Initialize $this->array_template from the first row in the CSV.

Returns
$this

Definition at line 145 of file CsvToArray.php.

145  {
146 
147  $this->SplFileObj->seek(0);
148  $this->array_template = str_getcsv($this->SplFileObj->current(), $this->delimiter, $this->enclosure);
149 
150  return $this;
151  }

◆ SetFileObject()

CsvToArray::SetFileObject (   $file)
protected

Instantiate the native SplFileObject. We could call it directly, setting it as a property makes it a little more visible.

Parameters
string$file
Returns
$this

Definition at line 134 of file CsvToArray.php.

134  {
135 
136  $this->SplFileObj = new SplFileObject($file);
137 
138  return $this;
139  }

The documentation for this class was generated from the following file: