HummingBirdAnimeClient/src/Aviat/Ion/Friend.php

121 lines
2.2 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-06 11:38:20 -04:00
/**
* Object to create a friend of
* @var object
*/
2015-11-04 16:36:54 -05:00
private $_friend_;
2015-10-06 11:38:20 -04:00
/**
* Reflection class of the object
* @var object
*/
2015-11-04 16:36:54 -05:00
private $_reflect_;
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");
}
2015-11-04 16:36:54 -05:00
$this->_friend_ = $obj;
$this->_reflect_ = new ReflectionClass($obj);
2015-09-25 13:41:12 -04:00
}
/**
* Retrieve a friend's property
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
2015-11-04 16:36:54 -05:00
if ($this->_reflect_->hasProperty($key))
2015-09-25 13:41:12 -04:00
{
$property = $this->_get_property($key);
2015-11-04 16:36:54 -05:00
return $property->getValue($this->_friend_);
2015-09-25 13:41:12 -04:00
}
return NULL;
}
/**
* Set a friend's property
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
2015-11-04 16:36:54 -05:00
if ($this->_reflect_->hasProperty($key))
2015-09-25 13:41:12 -04:00
{
$property = $this->_get_property($key);
2015-11-04 16:36:54 -05:00
$property->setValue($this->_friend_, $value);
2015-09-25 13:41:12 -04:00
}
}
/**
* Calls a protected or private method on the friend
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
2015-11-04 16:36:54 -05:00
if ( ! $this->_reflect_->hasMethod($method))
2015-09-25 13:41:12 -04:00
{
throw new BadMethodCallException("Method '{$method}' does not exist");
}
2015-11-04 16:36:54 -05:00
$friendMethod = new ReflectionMethod($this->_friend_, $method);
2015-09-25 13:41:12 -04:00
$friendMethod->setAccessible(TRUE);
2015-11-04 16:36:54 -05:00
return $friendMethod->invokeArgs($this->_friend_, $args);
2015-09-25 13:41:12 -04:00
}
/**
* 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-11-04 16:36:54 -05:00
$property = $this->_reflect_->getProperty($name);
2015-10-01 16:02:51 -04:00
$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
2015-11-09 11:10:15 -05:00
catch (\Exception $e)
{
return NULL;
}
2015-09-25 13:41:12 -04:00
return NULL;
}
}
// End of Friend.php