GithubHelp home page GithubHelp logo

guilhermegonzaga / presenter Goto Github PK

View Code? Open in Web Editor NEW
46.0 2.0 8.0 28 KB

Implementation for Laravel of the presenter design pattern.

License: MIT License

PHP 100.00%
laravel php presenter view entity

presenter's Introduction

Laravel Presenter

Latest Stable Version Total Downloads Latest Unstable Version License PHPUnit Test

Presenter is a design pattern for Laravel which is used to modify the data that comes from your model to your views.
It causes the data to be displayed in a way understandable to humans.

Installation

Laravel (5.x|6.x|7.x|8.x|9.x|10.x|11.x)

Execute the following command to get the latest version of the package:

composer require guilhermegonzaga/presenter

Usage

The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required.
Note that your presenter class must extend Laracodes\Presenter\Presenter:

namespace App\Presenters;

use Laracodes\Presenter\Presenter;

class UserPresenter extends Presenter
{
    public function fullName()
    {
        return $this->model->first_name . ' ' . $this->model->last_name;
    }
    
    public function accountAge()
    {
        return $this->model->created_at->diffForHumans();
    }

    ...
}

Next, on your model, pull in the Laracodes\Presenter\Traits\Presentable trait, which will automatically instantiate your presenter class:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laracodes\Presenter\Traits\Presentable;

class User extends Model
{
    use Presentable;
    
    protected $presenter = 'App\Presenters\UserPresenter';

    ...
}

Done, now you can call it in your views:

<h1>Hello, {{ $user->present()->fullName }}</h1>
<h1>Hello, {{ $user->present()->full_name }}</h1> // automatically convert to camelCase

Notice how the call to the present() method (which will return your new or cached presenter object) also provides the benefit of making it perfectly clear where you must go, should you need to modify how a full name is displayed on the page.

Notices

When you call a method that does not exist in its class presenter, this package will automatically call the property in the model with conversion to snake_case.

Ex:

// automatically calls the property in the model
<h1>Hello, {{ $user->present()->firstName }}</h1> // automatically convert to snake_case
<h1>Hello, {{ $user->present()->first_name }}</h1>

Credits

This package is largely inspired by this great package by @laracasts.

presenter's People

Contributors

conroyp avatar gc-max avatar guilhermegonzaga avatar jaybizzle avatar maxgiting avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

presenter's Issues

Presenter using on pivot

I can't find a way to make presenter working with a belongsToMany relation (pivot).

Call to undefined method Illuminate\Database\Query\Builder::present()

Laravel 8

Hi @guilhermegonzaga I wonder if you would mind changing this to allow for use with Laravel 8 if it's not too much work.

Thanks.

Fixing the broken tests

Hi @guilhermegonzaga

I've been taking a look to the broken test and I would like to suggest you how to fix it.

The GitHub action running your tests is configured to use the Ubuntu latest image and the default PHP version there.

Before Ubuntu 22.04, the default PHP version was 7.x and your tests were passing.

Then, GitHub started to use Ubuntu 22.04 and the default PHP version is 8.0. When you run your tests (using PHPUnit v5) they try to use a function that has been removed from PHP (the each()). And by this, the tests are broken.

How to fix it

There are several ways, let me suggest the one that I believe is the best.

  1. Create a branch of the current code (eg. 1.x)
  2. This branch will use PHP up to 7.x to run tests (I recommend to setup a new GHA for this branch)
  3. The master branch will use the current GHA version
  4. You need to refactor de test to be compliant with PHPUnit 9.x. It's really easy and I can submit a PR ๐Ÿ˜„

The branch and tag v1.x will be used for Laravel versions up to 8.

The master branch and maybe tag v2.x can be used for Laravel versions 9 and 10, which use PHP 8.x

What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.