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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package transformers

Transformers

Codacy Badge Build Status

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.

Logaretm\Transformers\Providers\TransformerServiceProvider::class

php artisan vendor:publish --provider="Logaretm\Transformers\Providers\TransformerServiceProvider" --tags="config"

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:

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

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.


All versions of transformers with dependencies

PHP Build Version
Package Version
Requires illuminate/support Version ^5.2
illuminate/console Version ^5.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package logaretm/transformers contains the following files

Loading the files please wait ....