Download the PHP package tooleks/laravel-presenter without Composer
On this page you can find all versions of the php package tooleks/laravel-presenter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tooleks/laravel-presenter
More information about tooleks/laravel-presenter
Files in tooleks/laravel-presenter
Package laravel-presenter
Short Description The Laravel Presenter Package
License MIT
Homepage https://github.com/tooleks/laravel-presenter
Informations about the package laravel-presenter
The Laravel Presenter Package
The package provides the Presenter
layer for wrapping model objects into new presentations.
Features
The package supports:
- Objects and arrays presentation
- Data mapping
- Nested attributes
- Attributes overriding
- The Laravel 5.2 collections
- JSON serialization
- Casting to array/JSON
- Injections into the constructor
Requirements
"php": "^7.0", "illuminate/support": "^5.2", "illuminate/contracts": "^5.2"
Installation
Package Installation
Execute the following command to get the latest version of the package:
App Configuration
To register the service provider simply add the Tooleks\Laravel\Presenter\Providers\PresenterProvider::class
into your config/app.php
to the end of the providers
array:
Usage Examples
Model Presentation
To define your presenter class, you need to extend base Tooleks\Laravel\Presenter\Presenter
class, as shown in the example below.
Override the getAttributesMap()
method to build attributes map definition.
Create a presenter object instance by passing a wrapped model into a setWrappedModel
method and use it like an object with nickname
, short_name
, full_name
, role
attributes. The wrapped model may be an array
or an object
.
Collection Presentation
The package also provides the collection macros method present()
for wrapping each item in the collection into a presenter class.
Data Mapping
The package provides a simple mechanism for data mapping. All you need is to call toArray()
method on the presenter object instance.
Using as a Response Data
The Tooleks\Laravel\Presenter\Presenter
class implements Illuminate\Contracts\Support\Arrayable
, Illuminate\Contracts\Support\Jsonable
, JsonSerializable
interfaces, so you may pass objects of this class directly into the response.
Tests
Execute the following command to run tests: