Note: This package is part of the Zaltana components, a serie of small packages made to provide useful features to Laravel projects.
This package provides a presentation layer for transforming data output when building an API.
- PHP 5.6+
- Laravel 5.2+
Pull this package in through Composer, by updating the composer.json
file as follows:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/marfurt/zaltana-transformers"
}
],
"require": {
"zaltana/transformers": "~1.0"
}
}
Override the abstract Transformer
class to create a custom transformer for your entity:
use Zaltana\Transformers\Transformer;
class MyModelTransformer extends Transformer {
protected function transform($model)
{
return [
'title' => $model->title,
'is_active' => $model->is_active
];
}
}
For transforming a model object, you need to instantiate your transformer and to call the process
method on it.
If you want to transform your model relationships as well, you need to inject their corresponding transformers into your model transformer.
$transformer = new MyModelTransformer([
'myRelationship' => new RelationshipTransformer()
]);
$data = $transformer->process($myModelObject);
You can also transform a collection of objects instead of a single object.
// Transforming array of objects
$objectsInArray = [ $myModelObject ];
$data = $transformer->process($objectsInArray);
// Transforming a Laravel Collection of objects
$objectsInCollection = collect([ $myModelObject ]);
$data = $transformer->process($objectsInCollection);
You can also use the Transformable
trait on your model.
use Zaltana\Transformers\Transformable;
class MyModel extends Model {
use Transformable;
}
Then you can transform you objects as follows:
$transformer = new MyModelTransformer();
$data = $myModelObject->transform($transformer);
If you only use one transformer on your model, you can define a transformer via the $transformer
property.
$myModelObject->transformer = new MyModelTransformer();
$data = $myModelObject->transform();
If you don't define any transformer on your model and call transform()
, it will dynamically look for a default transformer named ModelClassNameTransformer
in the same namespace.
If no transformer is found, a TransformerException exception is thrown.
$object = new MyModel();
$data = $object->transform(); // Will try to use MyModelTransformer
##License
This library is open-sourced software licensed under the MIT license.