2017 © Pedro Peláez
 

library formatter

Convert objects into arrays of data by applying processors, filters, and sorters.

image

graze/formatter

Convert objects into arrays of data by applying processors, filters, and sorters.

  • Thursday, April 7, 2016
  • by graze
  • Repository
  • 12 Watchers
  • 5 Stars
  • 7,022 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 7 % Grown

The README.md

graze/formatter Build Status Latest Version MIT Licensed

Convert objects into arrays of data by applying processors, filters, and sorters., (*1)

Read more about why we made this library in our blog post., (*2)

Installation

We recommend installing this library with Composer., (*3)

~$ composer require graze/formatter

Usage

// Create a formatter ...
class CountableFormatter extends \Graze\Formatter\AbstractFormatter
{
    protected function convert($object)
    {
        if (! $object instanceof Countable) {
            throw new \InvalidArgumentException(sprintf('`$object` must be an instance of %s.', Countable::class));
        }

        return [
            'count' => $object->count(),
        ];
    }
}

// ... processor ...
$processor = function (array $data, Countable $object) {
    // Let's add the square of count.
    $data['square'] = $data['count'] ** 2;

    return $data;
};

// ... filter ...
$filter = function (array $data) {
    // Remove elements with a square of 9.
    return $data['square'] !== 9;
};

// ... sorter ...
$sorter = function (array $data) {
    // Sort by count in descending order.
    return $data['count'] * -1;
};

// ... and something we can format.
class ExampleCountable implements Countable
{
    private $count = 0;

    public function count()
    {
        return $this->count += 1;
    }
}

$countable = new ExampleCountable();

// Create a new instance of the formatter, and register all the callables.
$formatter = new CountableFormatter();
$formatter->addProcessor($processor);
$formatter->addFilter($filter);
$formatter->addSorter($sorter);

// Format a single object.
$result = $formatter->format($countable);

print_r($result);

// Format several objects.
$result = $formatter->formatMany([$countable, $countable, $countable]);

print_r($result);

The above example will output:, (*4)

Array
(
    [count] => 1
    [square] => 1
)
Array
(
    [0] => Array
        (
            [count] => 4
            [square] => 16
        )

    [1] => Array
        (
            [count] => 2
            [square] => 4
        )

)

Find more documentation under docs/., (*5)

The Versions

07/04 2016

dev-master

9999999-dev

Convert objects into arrays of data by applying processors, filters, and sorters.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Simon Lawrence

07/04 2016

v0.2.0

0.2.0.0

Convert objects into arrays of data by applying processors, filters, and sorters.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Simon Lawrence

07/04 2016

dev-php55-support

dev-php55-support

Convert objects into arrays of data by applying processors, filters, and sorters.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Simon Lawrence

18/12 2015

dev-docs-patch

dev-docs-patch

Convert objects into arrays of data by applying processors, filters, and sorters.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Simon Lawrence

09/12 2015

v0.1.0

0.1.0.0

:gem: Interface for formatting objects into arrays.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Simon Lawrence