1. Go to this page and download the library: Download pustato/presenter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pustato / presenter example snippets
use Pustato\Presenter\Contracts\PresentableContract;
class SomeModel implements PresentableContract {
/** @var int */
public $id;
/** @var string */
public $firstName;
/** @var string */
public $lastName;
/** @var \DateTime */
public $birthDate;
public function getPresentableAttribute(string $name)
{
return property_exists($this, $name) ? $this->$name : null;
}
}
use Pustato\Presenter\AbstractPresenter;
use Pustato\Presenter\Contracts\PresentableContract;
class SomeModelPresenter extends AbstractPresenter {
protected function attributes(PresentableContract $presentable): array
{
return [
'fullName' => $presentable->firstName.' '.$presentable->lastName,
// callable will be calculated at first call and cached.
'age' => function($presentable) {
return (int) $presentable
->birthDate
->diff(new \DateTime('now'))
->y;
}
];
}
}
$presenterInstance = new SomeModelPresenter($modelInstance)