Download the PHP package ayeaye/formatters without Composer
On this page you can find all versions of the php package ayeaye/formatters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ayeaye/formatters
More information about ayeaye/formatters
Files in ayeaye/formatters
Package formatters
Short Description Serialise PHP objects into other types of object (json, xml, etc)
License MIT
Homepage http://ayeayeapi.com
Informations about the package formatters
Aye Aye Formatters
[]
(https://php.net/)
[
]
(https://raw.githubusercontent.com/AyeAyeApi/Formatters/master/LICENSE.txt)
[
]
(https://packagist.org/packages/ayeaye/formatters)
[
]
(https://travis-ci.org/AyeAyeApi/Formatters/branches)
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
The best way to install Aye Aye Formatters is with composer
Formatting Data
To format data into any specific serialised data format, just instantiate the formatter and pass it the data.
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:
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.
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.
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
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.