https://github.com/frostealth/php-presenter
Simple View Presenters
https://github.com/frostealth/php-presenter
php presenter view
Last synced: about 1 year ago
JSON representation
Simple View Presenters
- Host: GitHub
- URL: https://github.com/frostealth/php-presenter
- Owner: frostealth
- License: mit
- Created: 2015-12-12T17:55:52.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-14T11:48:03.000Z (over 10 years ago)
- Last Synced: 2024-12-25T07:22:30.812Z (over 1 year ago)
- Topics: php, presenter, view
- Language: PHP
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
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.
* 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.
## Installation
Run the [Composer](http://getcomposer.org/download/) command to install the latest stable version:
```bash
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.
Here's an example of a presenter.
```php
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.
```php
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:
```php
- Name
- = $model->presenter()->fullName ?>
- Birth Date
- = $model->presenter()->birthDate ?>
```
## License
The MIT License (MIT).
See [LICENSE.md](https://github.com/frostealth/php-presenter/blob/master/LICENSE.md) for more information.