Parse loan CSV data
Implements a CSV parser to convert loan CSV data into a proper array that may be used with the API. The parse implementation provides simple data type checking, to ensure in some cases proper values are passed (for instance it converts boolean true|false to string '0'|'1' as that's the expected value in the API). It also provides two static functions to create ready to be used parser instances: withHeaders and withFile.
withHeaders creates a parser from an array of header information:
$parser = CSVLoanParser(array('foo', 'bar', 'baz'));
withFile creates a parser from a CSV file, and treats the first line as header information so it's not necessary to set it independently:
$parser = CSVLoanParser(array('foo', 'bar', 'baz'));
withHeaders(array $headers) : \APAPI\Parser\CSVLoanParser
Create a new parser instance using the header information passed as argument.
array
The headers to use
\APAPI\Parser\CSVLoanParser
withFile(string $file) : \APAPI\Parser\CSVLoanParser|bool
Create a new parser instance using a file as starting point. The first line is considered header information. Returns the new instance, or false otherwise.
string
Path to the file to use
\APAPI\Parser\CSVLoanParser|bool
parse(array $row) : array|bool
Concrete implementation of the parse function. Parses a data row, performs type conversion when necessary. Fields not present in the valid fields array are not included in the resulting array. If there's an exception parsing the data row, returns false
array
The data row to parse
array|bool
setHeaders(array $headers)
Set the headers to use
array
The headers
getHeaders() : array
Get the current headers in use
array
parseFile(string $file) : array|bool
Reads a CSV file and executes a concrete parse method on each one, returning an array of parsed rows (or false if the file can't be read). The first line is not parsed, as it's considered headers which must be set prior to calling parseFile via setHeaders
string
Path to the CSV file on disc
array|bool
headers
Stores headers in use by this parser