|
Odyssey
|
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 = [] | |
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.
| CsvToArray::__construct | ( | $csv_path = '', |
|
$delimiter = ',', |
|||
$enclosure = null |
|||
| ) |
Constructor, initialize response and set path to CSV. Set delimiter and enclosure first, setArrayTemplate() uses them.
| string | $csv_path | |
| string | $delimiter | |
| string | $enclosure |
Definition at line 57 of file CsvToArray.php.
| 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.
Definition at line 204 of file CsvToArray.php.
|
protected |
Leverage str_getcsv()to parse the CSV file.
| string | $line |
Definition at line 158 of file CsvToArray.php.
|
protected |
Map the data to the first row of fields so we have an associative array.
| array | $data |
Definition at line 194 of file CsvToArray.php.
|
protected |
Validate we can do array_combine
| array | $data |
Definition at line 184 of file CsvToArray.php.
|
protected |
|
protected |
| CsvToArray::IsLinesToProcess | ( | ) |
Check that the SplFileObject is still an object (not null)
Definition at line 91 of file CsvToArray.php.
| CsvToArray::ParseData | ( | ) |
Get the associative array of $this->array_template mapped to CSV data rows.
Definition at line 71 of file CsvToArray.php.
|
protected |
Return a response, this approach allows chaining of methods above it.
Definition at line 217 of file CsvToArray.php.
|
protected |
A typical setter, sets internal properties (delimiter, enclosure) Usually setters are public, don't think we'll need it externally
| string | $prop | property identifier |
| string | $value | property to set |
Definition at line 117 of file CsvToArray.php.
|
protected |
Initialize $this->array_template from the first row in the CSV.
Definition at line 145 of file CsvToArray.php.
|
protected |
Instantiate the native SplFileObject. We could call it directly, setting it as a property makes it a little more visible.
| string | $file |
Definition at line 134 of file CsvToArray.php.
1.8.15