PHPackages                             graze/formatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. graze/formatter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

graze/formatter
===============

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

v0.2.0(10y ago)625.4k↓41.7%1MITPHPPHP ^5.5 || ^5.6 || ^7.0

Since Dec 9Pushed 5y ago11 watchersCompare

[ Source](https://github.com/graze/formatter)[ Packagist](https://packagist.org/packages/graze/formatter)[ RSS](/packages/graze-formatter/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

graze/formatter [![Build Status](https://camo.githubusercontent.com/ef996719cd359f66bca1fa02adc346c23ffe92a01da03ed493829adca3fc8db1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6772617a652f666f726d61747465722f6d61737465722e737667)](https://travis-ci.org/graze/formatter) [![Latest Version](https://camo.githubusercontent.com/582c833a88c5f322e27ac1dcf904da66408547bd14d09135ca768d89f2a6a754/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6772617a652f666f726d61747465722e737667)](https://packagist.org/packages/graze/formatter) [![MIT Licensed](https://camo.githubusercontent.com/0c0bf1582cd05f9bcaf0bede054f1bcef53f6b1c8d540361a433921d4f89795e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6772617a652f666f726d61747465722e737667)](https://github.com/graze/formatter/blob/master/LICENSE)
================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#grazeformatter---)

Convert objects into arrays of data by applying [processors](docs/01-processors.md), [filters](docs/02-filters.md), and [sorters](docs/03-sorters.md).

Read more about why we made this library in our [blog post](http://tech.graze.com/2015/12/10/graze-formatter/).

Installation
------------

[](#installation)

We recommend installing this library with [Composer](https://getcomposer.org).

```
~$ composer require graze/formatter
```

Usage
-----

[](#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:

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

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

)

```

Find more documentation under [`docs/`](/docs).

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~119 days

Total

2

Last Release

3729d ago

PHP version history (2 changes)v0.1.0PHP ^5.6 || ^7.0

v0.2.0PHP ^5.5 || ^5.6 || ^7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/637788?v=4)[graze.com](/maintainers/graze)[@graze](https://github.com/graze)

---

Top Contributors

[![sjparkinson](https://avatars.githubusercontent.com/u/51677?v=4)](https://github.com/sjparkinson "sjparkinson (7 commits)")[![notlee](https://avatars.githubusercontent.com/u/10405691?v=4)](https://github.com/notlee "notlee (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/graze-formatter/health.svg)

```
[![Health](https://phpackages.com/badges/graze-formatter/health.svg)](https://phpackages.com/packages/graze-formatter)
```

###  Alternatives

[laracasts/utilities

Transform your PHP to JavaScript

2.2k10.3M54](/packages/laracasts-utilities)[graze/data-structure

Data collections and containers

12293.1k10](/packages/graze-data-structure)[wordpress/php-mcp-schema

PHP DTOs for the Model Context Protocol (MCP) specification

1245.4k1](/packages/wordpress-php-mcp-schema)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
