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(10y ago)31.2k1[3 issues](https://github.com/AyeAyeApi/Formatters/issues)2MITPHPPHP ^5.5 || ^7.0

Since Aug 20Pushed 10y 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 today

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 47% 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

3694d 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://avatars.githubusercontent.com/u/5105785?v=4)[AyeAye](/maintainers/AyeAye)[@ayeaye](https://github.com/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

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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