CSV/TSV Reader
Inphinit\Experimental\Delimited\Csv and Inphinit\Experimental\Delimited\Tsv can read CSV and TSV files with backward compatibility support.
CSV (Comma-Separated Values) and TSV (Tab-Separated Values) are plain-text formats for storing tabular data. They are widely adopted because they are simple, lightweight, and easily portable across different platforms.
Read CSV
A file containing the following content:
Name,Age,Location
Autumn,25,New York
Olivia 30,Seul
Josh,35,Singapore
It can be read as follows:
use Inphinit\Experimental\Delimited\Csv;
$handle = new Csv('file.csv');
echo '<h2>Headers:</h2>';
echo '<pre>';
var_dump($handle->getHeaders());
echo '</pre>';
echo '<h2>Contents:</h2>';
echo '<pre>';
while ($line = $handle->fetch()) {
var_dump($line);
}
output:
Headers:
array(3) {
[0]=> string(4) "Name"
[1]=> string(3) "Age"
[2]=> string(8) "Location"
}
Contents:
array(3) {
[0]=> string(6) "Autumn"
[1]=> string(2) "25"
[2]=> string(8) "New York"
}
array(3) {
[0]=> string(6) "Olivia"
[1]=> string(2) "30"
[2]=> string(4) "Seul"
}
array(3) {
[0]=> string(4) "Josh"
[1]=> string(2) "35"
[2]=> string(9) "Singapore"
}
Read TSV
A file containing the following content:
Name Age Location
Autumn 25 New York
Olivia 30 Seul
Josh 35 Singapore
Pode ser lido assim:
use Inphinit\Experimental\Delimited\Tsv;
$handle = new Tsv('file.tsv');
echo '<h2>Headers:</h2>';
echo '<pre>';
var_dump($handle->getHeaders());
echo '</pre>';
echo '<h2>Contents:</h2>';
echo '<pre>';
while ($line = $handle->fetch()) {
var_dump($line);
}
Flags
The reading behavior can be changed using the Csv::setFlags() and Tsv::setFlags() methods.
$handle = new Csv('file.csv');
$handle->setFlags(Csv::MODE_COLUMN|Csv::SKIP_EMPTY|Csv::SKIP_HEADER);
while ($line = $handle->fetch()) {
var_dump($line);
}
output:
array(3) {
["Name"]=>string(6) "Autumn"
["Age"]=>string(2) "25"
["Location"]=>string(8) "New York"
}
array(3) {
["Name"]=>string(6) "Olivia"
["Age"]=>string(2) "30"
["Location"]=>string(4) "Seul"
}
array(3) {
["Name"]=>string(4) "Josh"
["Age"]=>string(2) "35"
["Location"]=>string(9) "Singapore"
}
| Constant | Description |
|---|---|
MODE_COLUMN
|
Rows will be returned as associative arrays, using headers as keys |
MODE_INDEX
|
Rows will be returned as indexed arrays (default) |
SKIP_EMPTY
|
Empty rows will be ignored and will not be returned by fetch() |
SKIP_HEADER
|
The row with headers will not be returned in fetch(), but will still be present in getHeaders() |
STRICT
|
Documents whose header has only one column, because no compatible separator was found, will be considered invalid |
Data Transfer Object (DTO)
It is possible to deserialize the rows to instances of a specified class:
use Model\User;
$handle = new Csv('file.csv');
$handle->setDataTransferObject(User::class);
object(Model\User)#39 (3) {
["Name"]=>string(6) "Autumn"
["Age"]=>string(2) "25"
["Location"]=>string(8) "New York"
}
Using stdClass:
use Model\User;
$handle = new Csv('file.csv');
$handle->setDataTransferObject('stdClass');
object(stdClass)#39 (3) {
["Name"]=>string(6) "Autumn"
["Age"]=>string(2) "25"
["Location"]=>string(8) "New York"
}
End-of-line
Some older CSV files may have line breaks that are unusual nowadays; in these cases, you can adjust the end-of-line:
$handle = new Csv('file.csv');
$handle->setEndOfLine("\r");
Converting
After reading and filtering a CSV/TSV file, you can export it and even convert it to other tabs or formats.
Converting to JSON
$handle = new Csv('file.csv');
$handle->converter()->json('output[index].json', false);
$handle = new Csv('file.csv');
$handle->converter()->json('output[pairs].json', true);
Converting to CSV
$handle = new Csv('file.csv');
// Changing the separator
$handle->converter()->csv('output.csv', ';');
// Changing the separator and the enclosure
$handle->converter()->csv('output.csv', ';', '"');
// Changing the separator, enclosure, and end-of-line
$handle->converter()->csv('output.csv', ';', '`', "\r\n");
Converting to TSV
$handle = new Csv('file.csv');
// Save to TSV format
$handle->converter()->tsv('output.tsv');
// Changing the end-of-line
$handle->converter()->tsv('output.tsv', "\r\n");
Converting to HTML and XML
It's possible to convert using DOMDocument, allowing you to create HTML or XML, for example:
use Inphinit\Http\Response;
use Inphinit\Experimental\Delimited\Csv;
$handle = new Csv('file.csv');
$xml = new DOMDocument;
$main = $xml->createElement('Main');
$xml->appendChild($main);
$handle->setFilter(function (array &$fields, $index) {
// Handle values present in the headers.
if ($index === 0) {
foreach ($fields as &$field) {
$field = preg_replace('#[^\w]+#', '-', $field);
$field = preg_replace('#-{2,}#', '-', $field);
}
} else {
foreach ($fields as &$field) {
$field = stripcslashes($field);
}
}
});
$handle->converter()->dom($main);
Response::type('application/xml');
echo $xml->saveXML();