WideFocus Feed Writer
, (*1)
This package contains models to write a feed., (*2)
Installation
Use composer to install the package., (*3)
$ 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., (*4)
Writer
The writer processes feed data., (*5)
<?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., (*6)
<?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:, (*7)
Foo: FooValue
Bar: BARVALUE
Foo: AnotherFooValue
Bar: ANOTHERBARVALUE