2020-03-11 23:04:01 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2020-03-12 11:45:11 -04:00
|
|
|
* Hummingbird Anime List Client
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-03-12 11:45:11 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-03-12 11:45:11 -04:00
|
|
|
* @package HummingbirdAnimeClient
|
2022-03-04 12:32:17 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshome.page>
|
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren
|
2020-03-11 23:04:01 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2020-03-12 11:45:11 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion;
|
|
|
|
|
|
|
|
use BadMethodCallException;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use ReflectionClass;
|
2022-03-03 17:26:09 -05:00
|
|
|
use ReflectionException;
|
2020-03-11 23:04:01 -04:00
|
|
|
use ReflectionMethod;
|
|
|
|
use ReflectionProperty;
|
2022-03-03 17:26:09 -05:00
|
|
|
use function is_object;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Friend class for testing
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
class Friend
|
|
|
|
{
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Object to create a friend of
|
|
|
|
*/
|
2022-01-07 12:33:01 -05:00
|
|
|
private mixed $_friend_;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reflection class of the object
|
|
|
|
*/
|
2020-12-10 15:59:37 -05:00
|
|
|
private ReflectionClass $_reflect_;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a friend object
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
2022-03-03 17:26:09 -05:00
|
|
|
* @throws ReflectionException
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
2022-01-07 12:33:01 -05:00
|
|
|
public function __construct(mixed $obj)
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
if ( ! is_object($obj))
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Friend must be an object');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_friend_ = $obj;
|
|
|
|
$this->_reflect_ = new ReflectionClass($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a friend's property
|
|
|
|
*/
|
2022-01-07 12:33:01 -05:00
|
|
|
public function __get(string $key): mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
if ($this->__isset($key))
|
|
|
|
{
|
|
|
|
$property = $this->_get_property($key);
|
|
|
|
|
|
|
|
if ($property !== NULL)
|
|
|
|
{
|
|
|
|
return $property->getValue($this->_friend_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if a property exists on the friend
|
|
|
|
*/
|
|
|
|
public function __isset(string $name): bool
|
|
|
|
{
|
|
|
|
return $this->_reflect_->hasProperty($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a friend's property
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-07 12:33:01 -05:00
|
|
|
public function __set(string $key, mixed $value)
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
if ($this->__isset($key))
|
|
|
|
{
|
|
|
|
$property = $this->_get_property($key);
|
|
|
|
|
|
|
|
if ($property !== NULL)
|
|
|
|
{
|
|
|
|
$property->setValue($this->_friend_, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls a protected or private method on the friend
|
|
|
|
*
|
|
|
|
* @throws BadMethodCallException
|
2022-03-03 17:26:09 -05:00
|
|
|
* @throws ReflectionException
|
|
|
|
* @return mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
public function __call(string $method, array $args)
|
|
|
|
{
|
|
|
|
if ( ! $this->_reflect_->hasMethod($method))
|
|
|
|
{
|
|
|
|
throw new BadMethodCallException("Method '{$method}' does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
$friendMethod = new ReflectionMethod($this->_friend_, $method);
|
|
|
|
$friendMethod->setAccessible(TRUE);
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $friendMethod->invokeArgs($this->_friend_, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over parent classes to get a ReflectionProperty
|
|
|
|
*/
|
|
|
|
private function _get_property(string $name): ?ReflectionProperty
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$property = $this->_reflect_->getProperty($name);
|
|
|
|
$property->setAccessible(TRUE);
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $property;
|
|
|
|
}
|
|
|
|
// Return NULL on any exception, so no further logic needed
|
|
|
|
// in the catch block
|
2020-03-12 12:04:20 -04:00
|
|
|
// @codeCoverageIgnoreStart
|
2022-03-03 13:25:10 -05:00
|
|
|
catch (\Exception)
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2020-03-12 12:04:20 -04:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-03-11 23:04:01 -04:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of Friend.php
|