tequilarapido/presenter

Simple and straightforward presenter implementation for Eloquent models.

v1.1.5 2020-03-03 13:13 UTC

README

Simple and straightforward presenter implementation for Eloquent models.

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage

68747470733a2f2f7331362e706f7374696d672e6f72672f7371327661386d6e702f70726573656e7465722e6a7067

Contents

Installation

You can install the package using composer

$ composer require tequilarapido/presenter

Usage

Let's assume we have a User model class

class User extends Model {
   protected $fillables = ['first_name', 'last_name'];
}
  • 1/ Create a presenter class like the following, and add presentation methods to it. For the example, we'll make a method that will return the user full name.
use Tequilarapido\Presenter\Presenter;

class UserPresenter extends Presenter {    
     public function name()
     {
        return $this->first_name . ' ' . $this->last_name;
     }
}

We can access model property directly inside the presenter class. We can also access them via the $model property

$this->first_name
// or 
$this->model->first_name
// or
$this->model->getAttribute('first_name')
  • 2/ you need to reference this class in the model using a public $presenter property and use the Prensentable trait.
use Tequilarapido\Presenter\Presentable;

class User extends Model {
    use Presentable;

   protected $fillables = ['first_name', 'last_name'];
   
   public $presenter = UserPresenter::class;
}
  • 3/ You can than get the presented value like so :
$user = User::find(1);

// Retreive as property 
$user->present()->name 

// you can alse call the method 
$user->present()->name()

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email :author_email instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.