HummingBirdAnimeClient/src/Aviat/Ion/Friend.php

109 lines
2.1 KiB
PHP
Raw Normal View History

2015-09-25 13:41:12 -04:00
<?php
namespace Aviat\Ion;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use InvalidArgumentException;
use BadMethodCallException;
/**
* Friend class for testing
*/
class Friend {
2015-10-01 16:02:51 -04:00
private $_friend_object_;
private $_reflection_friend_;
2015-09-25 13:41:12 -04:00
/**
* Create a friend object
*
* @param object $obj
*/
public function __construct($obj)
{
if ( ! is_object($obj))
{
throw new InvalidArgumentException("Friend must be an object");
}
$this->_friend_object_ = $obj;
$this->_reflection_friend_ = new ReflectionClass($obj);
}
/**
* Retrieve a friend's property
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if ($this->_reflection_friend_->hasProperty($key))
{
$property = $this->_get_property($key);
return $property->getValue($this->_friend_object_);
}
return NULL;
}
/**
* Set a friend's property
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
if ($this->_reflection_friend_->hasProperty($key))
{
$property = $this->_get_property($key);
$property->setValue($this->_friend_object_, $value);
}
}
/**
* Calls a protected or private method on the friend
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
if ( ! $this->_reflection_friend_->hasMethod($method))
{
throw new BadMethodCallException("Method '{$method}' does not exist");
}
$friendMethod = new ReflectionMethod($this->_friend_object_, $method);
$friendMethod->setAccessible(TRUE);
return $friendMethod->invokeArgs($this->_friend_object_, $args);
}
/**
* Iterates over parent classes to get a ReflectionProperty
*
2015-10-01 16:02:51 -04:00
* @codeCoverageIgnore
2015-09-25 13:41:12 -04:00
* @param string $name
* @return ReflectionProperty|null
*/
2015-10-01 16:02:51 -04:00
private function _get_property($name)
2015-09-25 13:41:12 -04:00
{
2015-10-01 16:02:51 -04:00
try
2015-09-25 13:41:12 -04:00
{
2015-10-01 16:02:51 -04:00
$property = $this->_reflection_friend_->getProperty($name);
$property->setAccessible(TRUE);
return $property;
2015-09-25 13:41:12 -04:00
}
2015-10-06 10:44:33 -04:00
// Return NULL on any exception, so no further logic needed
// in the catch block
catch (\Exception $e) {}
2015-09-25 13:41:12 -04:00
return NULL;
}
}
// End of Friend.php