widefocus/feed-writer

This package contains models to write a feed.

1.0.0 2017-04-27 15:20 UTC

This package is not auto-updated.

Last update: 2024-11-15 22:57:26 UTC


README

Build Status Latest Stable Version License

This package contains models to write a feed.

Installation

Use composer to install the package.

$ composer require widefocus/feed-writer

Usage

This package is meant to be used as basis for feed writer implementations. To do so a writer needs to be implemented.

Writer

The writer processes feed data.

<?php

use WideFocus\Feed\Writer\WriterInterface;
use WideFocus\Feed\Writer\WriterFieldInterface;
use WideFocus\Feed\Writer\WriterTrait;

class DebugWriter implements WriterInterface
{
    use WriterTrait;

    /**
     * @var WriterFieldInterface[]
     */
    private $fields;

    /**
     * Constructor.
     *
     * @param WriterFieldInterface[] $fields
     */
    public function __construct(array $fields)
    {
        $this->fields = $fields;
    }

    /**
     * Write an item to the feed.
     *
     * @param ArrayAccess $item
     *
     * @return void
     */
    protected function writeItem(ArrayAccess $item)
    {
        foreach ($this->fields as $field) {
            echo sprintf(
                "%s: %s\n",
                $field->getLabel(),
                $field->getValue($item)
            );
        }
    }

    /**
     * Initialize the feed.
     *
     * @return void
     */
    protected function initialize()
    {
    }

    /**
     * Finish the feed.
     *
     * @return void
     */
    protected function finish()
    {
    }
}

Writing the feed

The writer expects an iterator as it's input. The iterator is expected to contain items that implement the ArrayAccess interface.

<?php

use WideFocus\Feed\Writer\WriterField;

$items = new ArrayIterator(
    [
        new ArrayObject(['foo' => 'FooValue', 'bar' => 'BarValue']),
        new ArrayObject(['foo' => 'AnotherFooValue', 'bar' => 'AnotherBarValue'])
    ]
);

$fields = [
    new WriterField('foo', 'Foo'),
    new WriterField('bar', 'Bar', 'strtoupper')
];

$writer = new DebugWriter($fields);
$writer->write($items);

This would result in the following output:

Foo: FooValue
Bar: BARVALUE
Foo: AnotherFooValue
Bar: ANOTHERBARVALUE