codedungeon
11/12/2013 - 1:48 AM

Example Laravel 4 Presenter Template, supporting calling standard $table->field properties, or $table->method, or $table->method(parameter)

Example Laravel 4 Presenter Template, supporting calling standard $table->field properties, or $table->method, or $table->method(parameter)

http://paste.laravel.com/16LT

RAWFORKNEW
<?php
/* --- Laracasts Presenters Lesson Homework --- */
/*
In order to provide the ability to call a method on the object we need the __call() method
If we convert the existing __get() method to a __call() method then we can no longer call the method as a property
ie. $user->gravitar will throw an error that the property does not exist 
So we need both __call() and __get()
For flexibility the __get() method will handle properties but will also convert $user->gravitar to $user->gravitar()
If no method exists then __get() will call it as a property
The __call method accepts arguments with a Null default and either calls the method with or without arguments
Both magic methods check before executing the resource method or property because if it doesn't return anything
the user will simply get a blank page. This is probably where a custom exception needs to be thrown instead of the
text errors in my example
/*--- Presenter.php ---*/
<?php namespace Acme\Presenters;
abstract class Presenter {
    
    protected $resource;
    
    public function __construct($resource)
    {
        
        $this->resource = $resource;
    
    }
    public function __get($name)
    {
        //if called as a property it gets converted to a method call unless the property exists
        //$user->gravitar
        if(method_exists($this, $name))
        {
            return $this->{$name}();
        }
        
        if(property_exists($this, $name))
        {
            return $this->{$name};
        }
	// otherwise, we are accessing a standard object
        return $this->resource->{$name};
    }
    public function __call($name, $arguments = null)
    {
        //if called as a method or a method with arguments it gets called here
        //$user->gravitar() or $user->gravitar(40) 
        if(method_exists($this, $name))
        {
            if(!empty($arguments)) return $this->{$name}($arguments);
            return $this->{$name}();
        }
        
        if($this->resource->{$name})
        {
            return $this->resource->{$name}; 
        }
        return 'no such method';
        
    }
    
}
/*--- UserPresenter.php ---*/
<?php namespace Acme\Presenters;
class UserPresenter extends Presenter {
    //for testing the use of properties
    protected $dummyProperty = 'Hello big dummy';
    public function created_at()
    {
        return $this->resource->created_at->format('Y-m-d');
    }
    public function gravitar($arguments = null)
    {
        //build gravitar path with or without optional arguments
        return 'gravitar path with the following arguments: ' . $arguments;
    }
}
/*--- USAGE ---*/
//call as a property
$user->gravitar
//call as a method
$user->gravitar()
//call as a method with arguments
$user->gravitar(40)