* @copyright 2015 - 2018 Timothy J. Warren * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version 4.0 * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient */ namespace Aviat\AnimeClient\Types; use ArrayAccess; use LogicException; abstract class AbstractType implements ArrayAccess { /** * Sets the properties by using the constructor * * @param array $data */ public function __construct(array $data = []) { foreach ($data as $key => $value) { $this->$key = $value; } } /** * See if a property is set * * @param $name * @return bool */ public function __isset($name): bool { return property_exists($this, $name); } /** * Set a property on the type object * * @param string $name * @param mixed $value * @return void */ public function __set($name, $value): void { if (!property_exists($this, $name)) { $existing = json_encode($this); throw new LogicException("Trying to set non-existent property: '$name'. Existing properties: $existing"); } $this->$name = $value; } /** * Get a property from the type object * * @param string $name * @return mixed */ public function __get($name) { if (property_exists($this, $name)) { return $this->$name; } throw new LogicException("Trying to get non-existent property: '$name'"); } /** * Implementing ArrayAccess * * @param $offset * @return bool */ public function offsetExists($offset): bool { return $this->__isset($offset); } /** * Implementing ArrayAccess * * @param $offset * @return mixed */ public function offsetGet($offset) { return $this->__get($offset); } /** * Implementing ArrayAccess * * @param $offset * @param $value */ public function offsetSet($offset, $value): void { $this->__set($offset, $value); } /** * Implementing ArrayAccess * * @param $offset */ public function offsetUnset($offset): void { // Do nothing! } }