WideFocus Feed Csv Writer
, (*1)
This package contains models to write a CSV feed., (*2)
Installation
Use composer to install the package., (*3)
$ composer require widefocus/feed-csv-writer
Usage
First create a writer factory:, (*4)
<?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:, (*5)
<?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:, (*6)
<?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:, (*7)
Foo,Bar
FooValue,BARVALUE
AnotherFooValue,ANOTHERBARVALUE