PHP code example of andrewdyer / view-presenters

1. Go to this page and download the library: Download andrewdyer/view-presenters 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/ */

    

andrewdyer / view-presenters example snippets


$user = new App\Models\User;
$user->setForename('Andrew');
$user->setSurname('Dyer');

var_dump($user->present()->name); // Andrew Dyer

namespace App\Presenters;

use Anddye\ViewPresenters\Presenter;
use App\Models\User;

class UserPresenter extends Presenter
{
    protected User $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function defaultAttributes(): array
    {
        $data = [];
        $data['id'] = $this->user->getId();
        $data['forename'] = $this->user->getForename();
        $data['surname'] = $this->user->getSurname();

        return $data;
    }

    public function name(): string
    {
        return $this->user->getForename() . ' ' . $this->user->getSurname();
    }
}

namespace App\Models;

use Anddye\ViewPresenters\HasPresenters;
use App\Presenters\UserPresenter;
use App\Presenters\UserSubscriptionPresenter;

class User
{
    use HasPresenters;

    protected int $id;
    protected string $forename;
    protected string $surname;
    protected array $presenters = [
        'default' => UserPresenter::class,
        'subscription' => UserSubscriptionPresenter::class
        // ...
    ];

    // ...

}