2017 © Pedro Peláez
 

library php-presenter

Simple View Presenters

image

frostealth/php-presenter

Simple View Presenters

  • Monday, December 14, 2015
  • by frostealth
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,750 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 30 % Grown

The README.md

PHP View Presenters

So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) is displayed from the view., (*1)

  • Should that logic be hard-coded into the view? No.
  • Should we instead store the logic in the model? No again!

Instead, leverage view presenters. That's what they're for! This package provides one such implementation., (*2)

Installation

Run the Composer command to install the latest stable version:, (*3)

composer require frostealth/php-presenter @stable

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., (*4)

Here's an example of a presenter., (*5)

namespace app\presenters;

use frostealth\presenter\Presenter;

/**
 * Class ConcreteEntityPresenter
 *
 * @property-read string $fullName
 * @property-read string $birthDate
 */
class ConcreteModelPresenter extends Presenter
{
    /**
     * @return string
     */
    public function getFullName()
    {
        return implode(' ', [$this->firstName, $this->lastName]);
    }

    /**
     * @return string
     */
    public function getBirthDate()
    {
        return date('y.M.d', $this->entity->birthDate);
    }
}

Here's an example of an presentable model., (*6)

namespace app\models;

use app\presenters\ConcreteModelPresenter;
use frostealth\presenter\interfaces\PresentableInterface;

class ConcreteModel implements PresentableInterface
{
    /** @var string */
    public $firstName;

    /** @var string */
    public $lastName;

    /** @var string */
    public $birthDate;

    /** @var ConcreteModelPresenter */
    protected $presenter;

    /**
     * @return ConcreteModelPresenter
     */
    public function presenter()
    {
        if ($this->presenter === null) {
            $this->presenter = new ConcreteModelPresenter($this);
        }

        return $this->presenter;
    }
}

Now, within your view, you can do:, (*7)

<dl>
    <dt>Name</dt>
    <dd><?= $model->presenter()->fullName ?></dd>

    <dt>Birth Date</dt>
    <dd><?= $model->presenter()->birthDate ?></dd>
</dl>

License

The MIT License (MIT). See LICENSE.md for more information., (*8)

The Versions

14/12 2015

dev-master

9999999-dev

Simple View Presenters

  Sources   Download

MIT

The Requires

  • php >=5.4

 

presenter

14/12 2015

v0.1.1

0.1.1.0

Simple View Presenters

  Sources   Download

MIT

The Requires

  • php >=5.4

 

presenter

12/12 2015

v0.1.0

0.1.0.0

Simple View Presenters

  Sources   Download

MIT

The Requires

  • php >=5.4

 

presenter