CSV Reader and Writer

1.1.0 2016-03-04 19:48 UTC

This package is auto-updated.

Last update: 2024-10-23 09:56:17 UTC


README

Build Status Coverage Status

A CSV reader and writer with no external dependencies, that allows just in time formatting and filtering, and operates on files as well as streams.

Install

composer require phillipsdata/csv

Basic Usage

Reading a CSV

<?php

use PhillipsData\Csv\Reader;

// Set the input for the reader
$reader = Reader::input(new SplFileObject('php://stdin'));

$lines = [];
// Fetch the result for each line read
foreach ($reader->fetch() as $line) {
    $lines[] = $line;
}

Writing a CSV

<?php

use PhillipsData\Csv\Writer;

$writer = Writer::output(new SplFileObject('/path/to/file.csv'));

$data = [
    ['colA', 'colB'],
    ['A1', 'B1'],
    ['A2', 'B2']
];

// Write all rows (works great with Iterators)
$writer->write($data);

// Or, write a single row at a time
foreach ($data as $row) {
    $writer->writeRow($row);
}

Factories

<?php

use PhillipsData\Csv\Factory;

// returns \PhillipsData\Csv\Writer
$writer = Factory::writer('path/to/file', ',', '"', '\\');

// returns \PhillipsData\Csv\Reader
$reader = Factory::reader('path/to/file', ',', '"', '\\');

Formatting and Filtering

Formatting and Filtering work for both reading and writing.

To format data while iterating, simply specify the format callback function.

// The formatter is called for each line parsed
$reader->format(function ($line, $key, $iterator) {
    return [
        'first_name' => $line['First Name'],
        'last_name' => $line['Last Name'],
        'email' => strtolower($line['Email']),
        'date' => date('c', strtotime($line['Date']))
    ];
});

foreach ($reader->fetch() as $line) {
    // $line now contains 'first_name', 'last_name', 'email', and 'date'.
}

To filter data while iterating, simply specify the filter callback function.

// The formatter is called for each line parsed
$reader->filter(function ($line, $key, $iterator) {
    return $line['Last Name'] === 'Smith';
});

foreach ($reader->fetch() as $line) {
    // $line only contains records where $line['Last Name'] === 'Smith'
}