Download the PHP package logaretm/transformers without Composer
On this page you can find all versions of the php package logaretm/transformers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download logaretm/transformers
More information about logaretm/transformers
Files in logaretm/transformers
Package transformers
Short Description Simple laravel eloquent models transformers.
License MIT
Informations about the package transformers
Transformers
This a package that provides transformer (reducer/serializer) classes and traits for the Laravel eloquent models.
Install
Via Composer
Transformer:
A class responsible for transforming or reducing an object from one form to another then consumed.
Why would you use them?
Transformers are useful in API responses, where you want the ajax results to be in a specific form, by hiding attributes, exposing additional ones, or convert attribute types.
Also by delegating the responsibility of transforming models to a separate class make it easier to handle and maintain down the line.
Inspiration
Having seenJeffery Way's Laracasts video and reading the book Building APIs You Won't Hate, I wanted to create a simple package specific to laravel apps and because I needed this functionality in almost every project.
Usage
First you need to a transformer for your model. the transformer should extend the Transformer abstract class. And provide an implementation for the method.
Now you can use the transformer in multiple ways, inject it in your controller method and laravel IoC should instantiate it.
You can also instantiate it manually if you don't think DI is your thing.
Dynamic Transformation
You can also use the TransformableTrait
on your model and define a $transformer
property to be able to use the getTransformer()
method.
then you can get the transformer instance using:
which can be helpful if you want to dynamically transform models. but note that it will throw a TransformerException
if the returned instance isn't an instance of Transformer
.
Service Provider
You may find retrieving the transformer over and over isn't intuitive, you can use the TransformerServiceProvider
and a config file to define an array mapping each model or any class to a transformer class.
- Add this line to
config/app.php
in the service providers array.
Logaretm\Transformers\Providers\TransformerServiceProvider::class
- Run this artisan command:
php artisan vendor:publish --provider="Logaretm\Transformers\Providers\TransformerServiceProvider" --tags="config"
-
Head over to config/transformers.php and populate the array with your model/transformer pairs.
- Now you don't need to provide the
$transformer
property anymore on your model, nor implement the interface.
Note that the transformer resolution for the related model will prioritize the registered transformers.
Furthermore you can now use the static methods Transformer::make
and Transformer::canMake
to instantiate transformers for the models, using the trait is still helpful, but not required anymore.
Relations
It is also possible to transform a model along with their related models using the fluent method .
The related model transformer is resolved when:
-
If the service provider is registered, then it will be resolved from the config array.
- If the model implements the
Transformable
contract which is automated by theTransformableTrait
. it also needs to define the$transformer
property.
otherwise the model will be transformed using a simple toArray()
call.
you can also transform nested relations with the same syntax.
you can reset the transformer relations using $transformer->resetRelations()
which will remove the related models from the transformation. also note that any call to with
will reset the transformer automatically.
aside from collections you can transform a paginator, or a single object.
Alternate Transformations
You don't have to use only one transformation per transformer, for example you may need specific transformations for specific scenarios for the same model.
using the method setTransformation
you can override the transformation method to use another one you have defined on the transformer.
To use the alternate transformation:
or you can pass a closure as an alternate transformation method.
Note that the naming convention for the transformation method is {transformation_name}Transformation
.
any subsequent calls to transform
method will use that transformation instead.
Note that it will throw a TransformerException if the requested transformation does not exist.
to reset the transformation method use the resetTransformation
method.
or if you want to reset both relations and transformation method:
Generating Transformers
You can easily generate a transformer class using this artisan command:
which will create a basic transformer class in app/Transformers
directory, don't forget to put your transformations there.
Testing
Use php unit for testing.
TODO
- Improve the API and method names.
Maybe a console command to generate a transformer for a model.Use closures to override transformation.- Write more todos.
Contributing
All contributes will be fully credited.
Issues
If you discover any issues, email me at [email protected] or use the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.