PlumCsv includes CSV readers and writers for Plum. Plum is a data processing pipeline for PHP.
Developed by Florian Eckerstorfer in Vienna, Europe.
You can install Plum using Composer.
$ composer require plumphp/plum-csv
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.
You can use the
Plum\PlumCsv\CsvReader to read data from a
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
change the index of each read item. In addition you can use the
SkipFirstFilter to skip the header row. Both
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`, ',', '"');
Plum\PlumCsv\CsvWriter allows you to write the data into a
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
" respectively. In addition
setHeader() method can be used to define the names of the columns. It has to be called before the
$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
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);
Version 0.4 (28 October 2015)
- Check if item is array before auto-setting header
- #10 Allow injection of
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
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.