2017 © Pedro Peláez
 

library formatters

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

image

ayeaye/formatters

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

  • Sunday, May 22, 2016
  • by AyeAye
  • Repository
  • 0 Watchers
  • 2 Stars
  • 1,163 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

Aye Aye Formatters

[Minimum PHP Version] (https://php.net/) [License] (https://raw.githubusercontent.com/AyeAyeApi/Formatters/master/LICENSE.txt) [Version] (https://packagist.org/packages/ayeaye/formatters) [Build Status] (https://travis-ci.org/AyeAyeApi/Formatters/branches), (*1)

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., (*2)

Installation

The best way to install Aye Aye Formatters is with composer, (*3)

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

Formatting Data

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

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:, (*5)

use AyeAye\Formatter\Writer\Xml;

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

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

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., (*6)

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., (*7)

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

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., (*8)

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., (*9)

Formatters will recurse through objects in order to only collect only the data you wish to give them., (*10)

The Versions

22/05 2016
18/02 2016
17/02 2016

dev-release/1.0.0

dev-release/1.0.0 http://ayeayeapi.com

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

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

05/02 2016

1.0.0-rc.4

1.0.0.0-RC4 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

01/11 2015

1.0.0-rc.3

1.0.0.0-RC3 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

01/11 2015

dev-release/1.0.0-rc.3

dev-release/1.0.0-rc.3 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

01/09 2015

1.0.0-rc.2

1.0.0.0-RC2 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

31/08 2015

1.0.0-rc.1

1.0.0.0-RC1 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

27/02 2015

1.0.0-beta.1

1.0.0.0-beta1 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

19/02 2015

0.11.0

0.11.0.0 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

14/09 2014

0.10.0

0.10.0.0 http://ayeayeapi.com

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

29/08 2014

v0.9.2

0.9.2.0 http://github.com/Gisleburt/GF_Formatter

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

26/08 2014

v0.9.1

0.9.1.0 http://github.com/Gisleburt/GF_Formatter

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

21/08 2014

v0.9.0

0.9.0.0 http://github.com/Gisleburt/GF_Formatter

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.4.0

 

The Development Requires

  • gisleburt/formatter-test dev-master