2017 © Pedro Peláez
 

library feed-writer

This package contains models to write a feed.

image

widefocus/feed-writer

This package contains models to write a feed.

  • Thursday, April 27, 2017
  • by sjokki
  • Repository
  • 1 Watchers
  • 0 Stars
  • 44 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

WideFocus Feed Writer

Build Status Latest Stable Version License, (*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

The Versions

27/04 2017

dev-master

9999999-dev

This package contains models to write a feed.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Ashoka de Wit

27/04 2017

1.0.0

1.0.0.0

This package contains models to write a feed.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Ashoka de Wit