Skip to content

Commit

Permalink
converted indentation to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
robclancy-test committed Aug 14, 2013
1 parent b5b889b commit 76606f0
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 563 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ echo 'And again: ', $user['url'];

### Extending the Decorator

As of 1.2.x I have added in a decorator object. This object takes care of turning an object that has `PresentableInterface` into a `Presenter`.
By default, this is done with Laravel's `View` objects. The reasoning behind a new class instead of the previous implementation is so it can be better tested and also to allow you to extend it.
As of 1.2.x I have added in a decorator object. This object takes care of turning an object that has `PresentableInterface` into a `Presenter`.
By default, this is done with Laravel's `View` objects. The reasoning behind a new class instead of the previous implementation is so it can be better tested and also to allow you to extend it.
Here is an example of extending the `Decorator` so that instead of using the `PresentableInterface` and `getPresenter()` method you can use a public variable on the object called `$presenter`.

Note: these instructions are for Laravel usage.
Expand Down
12 changes: 6 additions & 6 deletions src/Robbo/Presenter/PresentableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

interface PresentableInterface {

/**
* Return a created presenter.
*
* @return Robbo\Presenter\Presenter
*/
public function getPresenter();
/**
* Return a created presenter.
*
* @return Robbo\Presenter\Presenter
*/
public function getPresenter();
}
346 changes: 173 additions & 173 deletions src/Robbo/Presenter/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,181 +2,181 @@

abstract class Presenter implements \ArrayAccess {

/**
* The object injected on Presenter construction.
*
* @var mixed
*/
protected $object;

/**
* The decorator instance so we can nest presenters. Underscores here to avoid conflicts
* if a presenter or object has "decorator" as a variable.
*
* @var \Robbo\Presenter\Decorator
*/
protected static $__decorator;

/**
* Create the Presenter and store the object we are presenting.
*
* @param mixed $object
*/
public function __construct($object)
{
$this->object = $object;
}

/**
* Get the decorator, if none exists then use the default. Underscores here to avoid conflicts
* if a presenter or object needs to use "getDecorator".
*
* @var \Robbo\Presenter\Decorator
*/
protected function __getDecorator()
{
if (is_null(static::$__decorator))
{
static::$__decorator = new Decorator;
}

return static::$__decorator;
}

/**
* This is so you can extend the decorator and inject it into the presenter at the class level so the
* new decorator will be used for nested presenters. Method name should be "setDecorator" however
* like above I want to make conflicts less likely.
*
* @param \Robbo\Presenter\Decorator
* @return void
*/
public static function setExtendedDecorator(Decorator $decorator)
{
static::$__decorator = $decorator;
}

/**
* Get the object we are wrapping.
*
* @return mixed
*/
public function getObject()
{
return $this->object;
}

/*
* This will be called when isset() is called via array access.
*
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset)
{
// We only check isset on the array, if it is an object we return true as the object could be overloaded
if (is_array($this->object))
{
return isset($this->object[$offset]);
}

return true;
}

/**
* Add ability to access properties like an array.
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}

/**
* Set variable or key value using array access.
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
if (is_array($this->object))
{
$this->object[$offset] = $value;
return;
}

$this->object->$offset = $value;
}

/**
* Unset a variable or key value using array access.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
{
if (is_array($this->object))
{
unset($this->object[$offset]);
return;
}

unset($this->object->$offset);
}

/**
* Pass any unknown varible calls to present{$variable} or fall through to the injected object.
*
* @param string $var
* @return mixed
*/
public function __get($var)
{
$method = 'present'.str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $var)));
if (method_exists($this, $method))
{
return $this->$method();
}

return $this->__getDecorator()->decorate(is_array($this->object) ? $this->object[$var] : $this->object->$var);
}

/**
* Pass any uknown methods through to the inject object.
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
if (is_object($this->object))
{
$value = call_user_func_array(array($this->object, $method), $arguments);

return $this->__getDecorator()->decorate($value);
}

throw new \BadMethodCallException("Method {$method} does not exist.");
}

/**
/**
* The object injected on Presenter construction.
*
* @var mixed
*/
protected $object;

/**
* The decorator instance so we can nest presenters. Underscores here to avoid conflicts
* if a presenter or object has "decorator" as a variable.
*
* @var \Robbo\Presenter\Decorator
*/
protected static $__decorator;

/**
* Create the Presenter and store the object we are presenting.
*
* @param mixed $object
*/
public function __construct($object)
{
$this->object = $object;
}

/**
* Get the decorator, if none exists then use the default. Underscores here to avoid conflicts
* if a presenter or object needs to use "getDecorator".
*
* @var \Robbo\Presenter\Decorator
*/
protected function __getDecorator()
{
if (is_null(static::$__decorator))
{
static::$__decorator = new Decorator;
}

return static::$__decorator;
}

/**
* This is so you can extend the decorator and inject it into the presenter at the class level so the
* new decorator will be used for nested presenters. Method name should be "setDecorator" however
* like above I want to make conflicts less likely.
*
* @param \Robbo\Presenter\Decorator
* @return void
*/
public static function setExtendedDecorator(Decorator $decorator)
{
static::$__decorator = $decorator;
}

/**
* Get the object we are wrapping.
*
* @return mixed
*/
public function getObject()
{
return $this->object;
}

/*
* This will be called when isset() is called via array access.
*
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset)
{
// We only check isset on the array, if it is an object we return true as the object could be overloaded
if (is_array($this->object))
{
return isset($this->object[$offset]);
}

return true;
}

/**
* Add ability to access properties like an array.
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}

/**
* Set variable or key value using array access.
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
if (is_array($this->object))
{
$this->object[$offset] = $value;
return;
}

$this->object->$offset = $value;
}

/**
* Unset a variable or key value using array access.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
{
if (is_array($this->object))
{
unset($this->object[$offset]);
return;
}

unset($this->object->$offset);
}

/**
* Pass any unknown varible calls to present{$variable} or fall through to the injected object.
*
* @param string $var
* @return mixed
*/
public function __get($var)
{
$method = 'present'.str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $var)));
if (method_exists($this, $method))
{
return $this->$method();
}

return $this->__getDecorator()->decorate(is_array($this->object) ? $this->object[$var] : $this->object->$var);
}

/**
* Pass any uknown methods through to the inject object.
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
if (is_object($this->object))
{
$value = call_user_func_array(array($this->object, $method), $arguments);

return $this->__getDecorator()->decorate($value);
}

throw new \BadMethodCallException("Method {$method} does not exist.");
}

/**
* Allow ability to run isset() on a variable
*
* @param string $name
* @return boolean
*/
public function __isset($name)
{
if (is_array($this->object))
{
return isset($this->object[$name]);
}
if (is_array($this->object))
{
return isset($this->object[$name]);
}

return isset($this->object->$name);
}
Expand All @@ -188,11 +188,11 @@ public function __isset($name)
*/
public function __unset($name)
{
if (is_array($this->object))
{
unset($this->object[$name]);
return;
}
if (is_array($this->object))
{
unset($this->object[$name]);
return;
}

unset($this->object->$name);
}
Expand Down
Loading

0 comments on commit 76606f0

Please sign in to comment.