Skip to content

plumphp/plum-csv

Repository files navigation

Plum

PlumCsv includes CSV readers and writers for Plum. Plum is a data processing pipeline for PHP.

Build Status Windows Build status Scrutinizer Code Quality Code Coverage StyleCI

Developed by Florian Eckerstorfer in Vienna, Europe.

Installation

You can install Plum using Composer.

$ composer require plumphp/plum-csv

Usage

Please refer to the Plum documentation for information about Plum in general.

Currently PlumCsv contains a reader and a writer for CSV files and uses League\CSV to actually read and write CSV files.

CsvReader

You can use the Plum\PlumCsv\CsvReader to read data from a .csv file.

use Plum\PlumCsv\CsvReader;

$reader = new CsvReader('countries.csv');

Optionally you can also pass the delimiter and enclosure to the constructor.

$reader = new CsvReader('countries.csv`, ',', '"');

Most CSV files have a header row. Because Plum processes a CSV file row by row you need to add HeaderConverter to change the index of each read item. In addition you can use the SkipFirstFilter to skip the header row. Both HeaderConverter and SkipFirstFilter are part of the core Plum package.

use Plum\Plum\Converter\HeaderConverter;
use Plum\Plum\Filter\SkipFirstFilter;

$workflow = new Workflow();
$workflow->addConverter(new HeaderConverter());
$workflow->addFilter(new SkipFirstFilter(1));
$reader = new CsvReader('countries.csv`, ',', '"');

CsvWriter

The Plum\PlumCsv\CsvWriter allows you to write the data into a .csv file.

use Plum\PlumCsv\CsvWriter;

$writer = new CsvWriter('foobar.csv', ',', '"');
$writer->prepare();
$writer->writeItem(['value 1', 'value 2', 'value 3');
$writer->finish();

The second and third argument of __construct() are optional and by default , and " respectively. In addition the setHeader() method can be used to define the names of the columns. It has to be called before the prepare().

$writer = new CsvWriter('foobar.csv');
$writer->setHeader(['column 1', 'column 2', 'column 3']);
$writer->prepare();

When you read data dynamically you probably don't want to set the header columns manually. You can call autoDetectHeader() to use the array keys of the first item written to CsvWriter as headers.

$writer = new CsvWriter('foobar.csv');
$writer->autoDetectHeader(); // Must be called before the first `writeItem()`

If you need to further configure the writer, you can inject an instance of League\Csv\Writer to Plum\PlumCsv\CsvWriter.

use Plum\PlumCsv\CsvWriter;
use League\Csv\Writer;

$csv = Writer::createFromFileObject(new SplFileObject('countries.csv', 'w'));
$csv->setNullHandlingMode(Writer::NULL_AS_EMPTY);
$writer = new CsvWriter($csv);

Change Log

Version 0.4 (28 October 2015)

  • Check if item is array before auto-setting header
  • #10 Allow injection of League\Csv\Writer

Version 0.3.1 (28 April 2015)

  • Fix Plum version

Version 0.3 (22 April 2015)

  • Add support for ReaderFactory

Version 0.2 (21 April 2015)

  • Fix Plum version

Version 0.1 (24 March 2015)

  • Initial release

License

The MIT license applies to plumphp/plum.json. For the full copyright and license information, please view the LICENSE file distributed with this source code.

About

CSV readers and writers for Plum, a data processing pipeline in PHP.

Resources

License

Stars

Watchers

Forks

Packages

No packages published