PHP code example of robclancy / presenter

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

    

robclancy / presenter example snippets



class UserPresenter extends Robbo\Presenter\Presenter {

	public function url()
	{
		return $this->id.'-'.$this->username;
	}
}


class UserPresenter extends Robbo\Presenter\Presenter {

	public function presentUrl()
	{
		return $this->id.'-'.$this->username;
	}
}


class User {
	// ...
}

class UserPresenter extends Robbo\Presenter\Presenter {

	// ...
}

$user = new User;

// handle stuff here

// make sure to "convert" to a presenter before the object gets to your views
$user = new UserPresenter($user);


// Can also create a presenter for arrays
$user = [
	'id' => 1,
	'username' => 'Robbo',
];

// same as before
$user = new UserPresenter($user);


class UserPresenter extends Robbo\Presenter\Presenter {

	// ...
}

class User implements Robbo\Presenter\PresentableInterface {

	/**
	 * Return a created presenter.
	 *
	 * @return Robbo\Presenter\Presenter
	 */
	public function getPresenter()
	{
		return new UserPresenter($this);
	}
}


$user = [
	'id' => 1,
	'username' => 'Robbo',
];

class UserPresenter extends Robbo\Presenter\Presenter {

	public function presentUrl()
	{
		// This will work exactly the same as previous examples
		return $this->id.'-'.$this->username;

		// You can also do this...
		return $this['id'].'-'.$this['username'];
	}
}

// Now we create a presenter much the same as before
$user = new UserPresenter($user);


// In our views we can use the $user as if it were still an array
echo 'Hello, ', $user['username'];

// Or even treat it like the object that it is
echo 'Hello, ', $user->username;

// And like other examples, we can present the url in the same way
echo 'The URL: ', $user->url;
echo 'And again: ', $user['url'];



use Robbo\Presenter\Decorator as BaseDecorator;

class Decorator extends BaseDecorator {

	/*
     * If this variable implements Robbo\Presenter\PresentableInterface then turn it into a presenter.
     *
     * @param  mixed $value
     * @return mixed $value
    */
    public function decorate($value)
    {
    	if (is_object($value) and isset($value->presenter))
    	{
    		$presenter = $value->presenter;
    		return new $presenter;
    	}

    	return parent::decorate($value);
    }
}


// In start/global.php

App::make('presenter.decorator', App::share(function($app)
{
	$decorator = new Decorator;

	Robbo\Presenter\Presenter::setExtendedDecorator($decorator);
	return $decorator;
}));

// In a service provider's 'register' method

$this->app['presenter.decorator'] = $this->app->share(function($app)
{
	$decorator = new Decorator;

	Robbo\Presenter\Presenter::setExtendedDecorator($decorator);
	return $decorator;
});