widefocus / feed-csv-writer
This package contains the implementation to write a CSV feed.
1.0.0
2017-04-29 08:43 UTC
Requires
- php: ^7.0
- league/csv: ^8.1
- league/flysystem: ^1.0
- widefocus/feed-writer: ^1.0
- widefocus/feed-writer-builder: ^1.0
- widefocus/parameters: ^1.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2024-11-06 00:17:33 UTC
README
This package contains models to write a CSV feed.
Installation
Use composer to install the package.
$ composer require widefocus/feed-csv-writer
Usage
First create a writer factory:
<?php use WideFocus\Feed\CsvWriter\CsvWriterFactory; use WideFocus\Feed\CsvWriter\LeagueCsv\LeagueCsvWriterFactory; use League\Flysystem\MountManager; use League\Flysystem\Filesystem; use League\Flysystem\Adapter\Local; $mountManager = new MountManager( ['local' => new Filesystem(new Local('/tmp'))] ); $writerFactory = new CsvWriterFactory( new LeagueCsvWriterFactory(), $mountManager );
Then create a writer based on parameters and fields:
<?php use WideFocus\Parameters\ParameterBag; use WideFocus\Feed\Writer\WriterField; $parameters = new ParameterBag([ 'destination' => 'local://my-feed.csv', 'include_header' => true ]); $writer = $writerFactory->create( $parameters, new WriterField('foo', 'Foo'), new WriterField('bar', 'Bar', 'strtoupper') );
Then write the feed:
<?php use ArrayIterator; use ArrayObject; $items = new ArrayIterator( [ new ArrayObject(['foo' => 'FooValue', 'bar' => 'BarValue']), new ArrayObject(['foo' => 'AnotherFooValue', 'bar' => 'AnotherBarValue']) ] ); $writer->write($items);
This would result in a file /tmp/my-feed.csv with the following contents:
Foo,Bar
FooValue,BARVALUE
AnotherFooValue,ANOTHERBARVALUE