PHP code example of yaro / presenter

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

    

yaro / presenter example snippets


namespace App\Presenters;

use Yaro\Presenter\AbstractPresenter;

class UserPresenter extends AbstractPresenter
{
    protected $arrayable = [
        'name',
        'profession'
    ];
}

protected $arrayable = [
    'name',
    'profession',
    'random_number', // <- a virtual key, that model doesnt contain
];

// just studly case your key and surround it with 'get' and 'Present'.
public function getRandomNumberPresent()
{
    return rand(11,22);
}

use Yaro\Presenter\PresenterTrait;

class User
{
    use PresenterTrait;
    // ...

    protected $presenter = \App\Presenters\UserPresenter::class;

}

class User
{
    use PresenterTrait;
    // ...

    public function getPresenterClass()
    {
        if ($this->isBlocked()) {
            return \App\Presenters\BlockedUserPresenter::class;
        }

        return \App\Presenters\UserPresenter::class;
    }

}

$user = User::first();

return response()->json(compact('user'));