Download the PHP package sportuondo/eralda without Composer
On this page you can find all versions of the php package sportuondo/eralda. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sportuondo/eralda
More information about sportuondo/eralda
Files in sportuondo/eralda
Package eralda
Short Description Dead simple object to array transformation library.
License MIT
Homepage https://www.kubicode.com
Informations about the package eralda
Dead simple object to array transformation library
The objective of this library is to centralize the logic in charge of transforming objects (usually models) into arrays in order to expose them through JSON. Eralda can transform any type of object (PHP standard objects, Laravel Eloquent models...) into an array by just indicating the desired property mapping.
Eralda also offers the possibility of transforming property values as well as embedding related elements (e.g. author → books).
Installation
You can install the package via composer:
Usage
Given the following objects:
We can define the following Transformer:
The $keysMap
property is used to define the mapping between the properties of the source object and the keys of the resulting array.
On the other hand, we have included a presenter to modify the resulting value of a property of the original object on the fly. The nomenclature to define presenters is composed of the keyword present
followed by the property name of the original object in Camel Case format.
To execute the transformation we would do the following:
The resulting array would look like this:
Transforming collections
In the same way that we transform a single object we can also transform a collection of them. Since we have an array of authors we could do the following:
To get something like this:
Embedding additional elements
We can indicate the elements that we want to embed making use of the property $embeds
:
Given the following book transformer:
We could modify our transformer of authors in the following way:
In this case we want to embed the books belonging to the author. The nomenclature to define embeds is composed of the keyword embed
followed by the name of the element we want to embed in Camel Case format.
The result would be something like this:
Testing
Credits
License
The MIT License (MIT). Please see License File for more information.