PHPackages                             ayeaye/formatters - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. ayeaye/formatters

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

ayeaye/formatters
=================

Serialise PHP objects into other types of object (json, xml, etc)

1.0.2(9y ago)31.2k1[3 issues](https://github.com/AyeAyeApi/Formatters/issues)2MITPHPPHP ^5.5 || ^7.0

Since Aug 20Pushed 9y agoCompare

[ Source](https://github.com/AyeAyeApi/Formatters)[ Packagist](https://packagist.org/packages/ayeaye/formatters)[ Docs](http://ayeayeapi.com)[ RSS](/packages/ayeaye-formatters/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (17)Used By (2)

Aye Aye Formatters
==================

[](#aye-aye-formatters)

\[[![Minimum PHP Version](https://camo.githubusercontent.com/599bc5c91bb804f9384b53af2a2d7143e7b787bf82a79f1773db76ef50e51ab5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e342d3838393242462e737667)](https://camo.githubusercontent.com/599bc5c91bb804f9384b53af2a2d7143e7b787bf82a79f1773db76ef50e51ab5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e342d3838393242462e737667)\] () \[[![License](https://camo.githubusercontent.com/e8e9dc7d5136f4f2dc542a28507e78111449cb892e94031b4e16530f8bc0030d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6179656179652f666f726d6174746572732e737667)](https://camo.githubusercontent.com/e8e9dc7d5136f4f2dc542a28507e78111449cb892e94031b4e16530f8bc0030d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6179656179652f666f726d6174746572732e737667)\] () \[[![Version](https://camo.githubusercontent.com/3cbb95a9c7f0e68e253beb881aa9d5fbe324d3bf53847f4f143db579519ea097/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6179656179652f666f726d6174746572732e737667)](https://camo.githubusercontent.com/3cbb95a9c7f0e68e253beb881aa9d5fbe324d3bf53847f4f143db579519ea097/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6179656179652f666f726d6174746572732e737667)\] () \[[![Build Status](https://camo.githubusercontent.com/491574167c4047d9ca258b8e343d45855dd6351a62a3ca0dd133d851c5420d76/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4179654179654170692f466f726d6174746572732f6d61737465722e737667)](https://camo.githubusercontent.com/491574167c4047d9ca258b8e343d45855dd6351a62a3ca0dd133d851c5420d76/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4179654179654170692f466f726d6174746572732f6d61737465722e737667)\] ()

Aye Aye Formatters are a tool that to streamline the conversion of PHP data (arrays or objects) into serialised data formats, such as JSON or XML. Other formats can be easily added by extending the Formatter abstract.

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

[](#installation)

The best way to install Aye Aye Formatters is with composer

```
composer require="ayeaye/formatters ^1.0.0" -n
```

Formatting Data
---------------

[](#formatting-data)

To format data into any specific serialised data format, just instantiate the formatter and pass it the data.

```
use AyeAye\Formatter\Writer\Json;

$data = ['boolean' => true];

$json = new Json();
echo $json->format($data); // {"boolean":true}
```

The fullFormat method always assumes you want a valid file output. If you want only a partial format, just use the `partialFormat()` method. For example:

```
use AyeAye\Formatter\Writer\Xml;

$data = ['boolean' => true];

$xml = new Xml();
echo $xml->format($data); // true
echo $xml->partialFormat($data); // true
```

The FormatFactory
-----------------

[](#the-formatfactory)

The above functionality isn't particularly useful on it's own, that's where the FormatFactory comes in. Using the Factory, we can predefine a set of Formatters and then choose one later.

```
use AyeAye\Formatter\FormatFactory;
use AyeAye\Formatter\Writer\Json;

$formatFactory = new FormatFactory([
    'json' => new Json(),                    // Instantiate
    'xml' => 'AyeAye\Formatter\Writer\Xml', // or don't
]);

$formatFactory->getFormatterFor('json'); // returns the same Json instance every time.
$formatFactory->getFormatterFor('xm'); // returns a new XML instance every time.
```

More importantly you can request a formatter using an array of possible formats. This is useful as there are multiple ways an HTTP request could tell you what data format is desired. The correct way of course is to use the `Accepts`header. However, you could use a file suffix. What if the request came through without either, or none that are known to you? Then a default would be appropriate.

```
use AyeAye\Formatter\FormatFactory;
use AyeAye\Formatter\Writer\Json;
use AyeAye\Formatter\Writer\Xml;

$xmlFormatter = new Xml();
$jsonFormatter = new Json();
$this->formatFactory = new FormatFactory([
    // xml
    'xml' => $xmlFormatter,
    'text/xml' => $xmlFormatter,
    'application/xml' => $xmlFormatter,
    // json
    'json' => $jsonFormatter,
    'application/json' => $jsonFormatter,
]);

$formatFactory->getFormatterFor([
    $_HEADER['Accepts'], // The header that requests a specific format
    getRequestSuffix(),  // A result of looking at suffix of the requested resource
    'json',              // A fallback option
]);
```

Creating New Formats
--------------------

[](#creating-new-formats)

If you wish to return data in a format that isn't the included Json, Jsonp or Xml (or, if you want to improve on those provided), you can do so by extending the FormatFactory abstract class. You should be aware of the serializable interface described below.

Customising Output
------------------

[](#customising-output)

A Serializable interface is provided to help customise the output. Any object implementing the interface just needs to return the data it wants to be serialised by the formatters from the method `ayeAyeSerialize()` in a keyed array much like Php's own JsonSerializable interface works.

Formatters will recurse through objects in order to only collect only the data you wish to give them.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~53 days

Total

13

Last Release

3647d ago

Major Versions

0.11.0 → 1.0.0-beta.12015-02-27

PHP version history (2 changes)v0.9.0PHP &gt;=5.4.0

1.0.0PHP ^5.5 || ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fbf50261ec5e1621b8902365164874dc657e709127d3cde97a1d258f93a8131a?d=identicon)[AyeAye](/maintainers/AyeAye)

---

Top Contributors

[![Gisleburt](https://avatars.githubusercontent.com/u/2369524?v=4)](https://github.com/Gisleburt "Gisleburt (107 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/ayeaye-formatters/health.svg)

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

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

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