widefocus/feed-csv-writer

This package contains the implementation to write a CSV feed.

1.0.0 2017-04-29 08:43 UTC

This package is not auto-updated.

Last update: 2024-04-09 21:40:46 UTC


README

Build Status Latest Stable Version License

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