2016-10-13 13:11:22 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-08-04 14:55:37 -04:00
|
|
|
/**
|
|
|
|
* Ion
|
|
|
|
*
|
|
|
|
* Building blocks for web development
|
|
|
|
*
|
2016-10-13 13:11:22 -04:00
|
|
|
* PHP version 7
|
2016-08-26 23:10:20 -04:00
|
|
|
*
|
2016-08-04 14:55:37 -04:00
|
|
|
* @package Ion
|
2016-08-26 23:10:20 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-02-22 15:42:09 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2016-08-26 23:10:20 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2017-02-22 15:42:09 -05:00
|
|
|
* @version 2.0.0
|
2016-08-26 23:10:20 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/ion
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion;
|
|
|
|
|
2016-10-20 20:08:11 -04:00
|
|
|
use BadMethodCallException;
|
|
|
|
use InvalidArgumentException;
|
2016-08-04 14:55:37 -04:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionMethod;
|
|
|
|
use ReflectionProperty;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Friend class for testing
|
|
|
|
*/
|
|
|
|
class Friend {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object to create a friend of
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $_friend_;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reflection class of the object
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $_reflect_;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a friend object
|
|
|
|
*
|
|
|
|
* @param object $obj
|
2016-08-26 23:10:20 -04:00
|
|
|
* @throws InvalidArgumentException
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
|
|
|
public function __construct($obj)
|
|
|
|
{
|
|
|
|
if ( ! is_object($obj))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException("Friend must be an object");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_friend_ = $obj;
|
|
|
|
$this->_reflect_ = new ReflectionClass($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a friend's property
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public function __get(string $key)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
if ($this->_reflect_->hasProperty($key))
|
|
|
|
{
|
|
|
|
$property = $this->_get_property($key);
|
|
|
|
return $property->getValue($this->_friend_);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a friend's property
|
|
|
|
*
|
|
|
|
* @param string $key
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param mixed $value
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public function __set(string $key, $value)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
if ($this->_reflect_->hasProperty($key))
|
|
|
|
{
|
|
|
|
$property = $this->_get_property($key);
|
|
|
|
$property->setValue($this->_friend_, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls a protected or private method on the friend
|
|
|
|
*
|
|
|
|
* @param string $method
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param array $args
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return mixed
|
2016-08-26 23:10:20 -04:00
|
|
|
* @throws BadMethodCallException
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public function __call(string $method, array $args)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
if ( ! $this->_reflect_->hasMethod($method))
|
|
|
|
{
|
|
|
|
throw new BadMethodCallException("Method '{$method}' does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
$friendMethod = new ReflectionMethod($this->_friend_, $method);
|
|
|
|
$friendMethod->setAccessible(TRUE);
|
|
|
|
return $friendMethod->invokeArgs($this->_friend_, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over parent classes to get a ReflectionProperty
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return ReflectionProperty|null
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
private function _get_property(string $name)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$property = $this->_reflect_->getProperty($name);
|
|
|
|
$property->setAccessible(TRUE);
|
|
|
|
return $property;
|
|
|
|
}
|
|
|
|
// Return NULL on any exception, so no further logic needed
|
|
|
|
// in the catch block
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of Friend.php
|