|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Decapsulator package. |
| 5 | + * |
| 6 | + * (c) Katarzyna Krasińska <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace ExOrg\Decapsulator; |
| 13 | + |
| 14 | +/** |
| 15 | + * ObjectDecapsulator. |
| 16 | + * Decapsulator for object. |
| 17 | + * Provider direct access to non-public properties and methods |
| 18 | + * of the class of the wrapped object. |
| 19 | + * |
| 20 | + * @package Decapsulator |
| 21 | + * @author Katarzyna Krasińska <[email protected]> |
| 22 | + * @copyright Copyright (c) 2015 Katarzyna Krasińska |
| 23 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 24 | + * @link http://github.com/exorg/decapsulator |
| 25 | + */ |
| 26 | +class ObjectDecapsulator |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Valid decapsulated object type. |
| 30 | + * |
| 31 | + * @var unknown |
| 32 | + */ |
| 33 | + const VALID_OBJECT_TYPE = 'object'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Decapsulated object. |
| 37 | + * |
| 38 | + * @var mixed |
| 39 | + */ |
| 40 | + private $object; |
| 41 | + |
| 42 | + /** |
| 43 | + * Reflection for decapsulated object. |
| 44 | + * |
| 45 | + * @var \ReflectionClass |
| 46 | + */ |
| 47 | + private $reflection; |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor not available for public usage. |
| 51 | + */ |
| 52 | + private function __construct() |
| 53 | + { |
| 54 | + // Default action. |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Build ObjectDecapsulator instance for given decapsulated object. |
| 59 | + * |
| 60 | + * @param mixed $object |
| 61 | + * @throws \InvalidArgumentException |
| 62 | + * @return \Decapsulator\ObjectDecapsulator |
| 63 | + */ |
| 64 | + public static function buildForObject($object) |
| 65 | + { |
| 66 | + if (self::objectIsValid($object)) { |
| 67 | + $decapsulator = self::createInstanceFromObject($object); |
| 68 | + |
| 69 | + return $decapsulator; |
| 70 | + } else { |
| 71 | + $message = 'Argument is not an object.'; |
| 72 | + $exception = new \InvalidArgumentException($message); |
| 73 | + |
| 74 | + throw $exception; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Magically set object choosen property value. |
| 80 | + * Called when property is set directly by the property name. |
| 81 | + * |
| 82 | + * @param string $name |
| 83 | + * @param mixed $value |
| 84 | + */ |
| 85 | + public function __set($name, $value) |
| 86 | + { |
| 87 | + $propertyExists = $this->propertyExists($name); |
| 88 | + |
| 89 | + if ($propertyExists) { |
| 90 | + $this->setProperty($name, $value); |
| 91 | + } else { |
| 92 | + $message = 'Property does not exist.'; |
| 93 | + $exception = new \InvalidArgumentException($message); |
| 94 | + |
| 95 | + throw $exception; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Magically get object choosen property value. |
| 101 | + * Called when property is get directly by the property name. |
| 102 | + * |
| 103 | + * @param string $name |
| 104 | + * @return mixed |
| 105 | + */ |
| 106 | + public function __get($name) |
| 107 | + { |
| 108 | + $propertyExists = $this->propertyExists($name); |
| 109 | + |
| 110 | + if ($propertyExists) { |
| 111 | + $value = $this->getProperty($name); |
| 112 | + |
| 113 | + return $value; |
| 114 | + } else { |
| 115 | + $message = 'Property does not exist.'; |
| 116 | + $exception = new \InvalidArgumentException($message); |
| 117 | + |
| 118 | + throw $exception; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Magically call object choosen method. |
| 124 | + * Called when method is called directly by the method name. |
| 125 | + * |
| 126 | + * @param string $name |
| 127 | + * @param array[mixed] $arguments |
| 128 | + * @return mixed |
| 129 | + */ |
| 130 | + public function __call($name, $arguments) |
| 131 | + { |
| 132 | + $methodExists = $this->methodExists($name); |
| 133 | + |
| 134 | + if ($methodExists) { |
| 135 | + $result = $this->callMethod($name, $arguments); |
| 136 | + |
| 137 | + return $result; |
| 138 | + } else { |
| 139 | + $message = 'Method does not exist.'; |
| 140 | + $exception = new \InvalidArgumentException($message); |
| 141 | + |
| 142 | + throw $exception; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Check object is valid instance of the class. |
| 148 | + * |
| 149 | + * @param mixed $object |
| 150 | + * @return bool |
| 151 | + */ |
| 152 | + private static function objectIsValid($object) |
| 153 | + { |
| 154 | + $objectType = gettype($object); |
| 155 | + $objectIsValid = ($objectType === self::VALID_OBJECT_TYPE); |
| 156 | + |
| 157 | + return $objectIsValid; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Create ObjectDecapsulator instance wrapped given object. |
| 162 | + * |
| 163 | + * @param mixed $object |
| 164 | + * @return \Decapsulator\ObjectDecapsulator |
| 165 | + */ |
| 166 | + private static function createInstanceFromObject($object) |
| 167 | + { |
| 168 | + $instance = new self(); |
| 169 | + $instance->setUpWithObject($object); |
| 170 | + |
| 171 | + return $instance; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Set-up instance properties with object data. |
| 176 | + * |
| 177 | + * @param mixed $object |
| 178 | + */ |
| 179 | + private function setUpWithObject($object) |
| 180 | + { |
| 181 | + $this->setObject($object); |
| 182 | + $this->setUpReflection(); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Set decapsulated object. |
| 187 | + * |
| 188 | + * @param mixed $object |
| 189 | + */ |
| 190 | + private function setObject($object) |
| 191 | + { |
| 192 | + $this->object = $object; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Set-up reflection for decapsulated object. |
| 197 | + */ |
| 198 | + private function setUpReflection() |
| 199 | + { |
| 200 | + $className = get_class($this->object); |
| 201 | + $this->reflection = new \ReflectionClass($className); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Check object property with given name exists. |
| 206 | + * |
| 207 | + * @param string $name |
| 208 | + * @throws \UnexpectedValueException |
| 209 | + * @return boolean |
| 210 | + */ |
| 211 | + private function propertyExists($name) |
| 212 | + { |
| 213 | + $className = get_class($this->object); |
| 214 | + $propertyExists = property_exists($className, $name); |
| 215 | + |
| 216 | + return $propertyExists; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Check object method with given name exists. |
| 221 | + * |
| 222 | + * @param string $name |
| 223 | + * @throws \UnexpectedValueException |
| 224 | + * @return boolean |
| 225 | + */ |
| 226 | + private function methodExists($name) |
| 227 | + { |
| 228 | + $methodExists = method_exists($this->object, $name); |
| 229 | + |
| 230 | + return $methodExists; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Set value of the given object property. |
| 235 | + * |
| 236 | + * @param string $name |
| 237 | + * @param mixed $value |
| 238 | + */ |
| 239 | + private function setProperty($name, $value) |
| 240 | + { |
| 241 | + $property = $this->reflection->getProperty($name); |
| 242 | + $property->setAccessible(true); |
| 243 | + $property->setValue($this->object, $value); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Get value of the given object property. |
| 248 | + * |
| 249 | + * @param string $name |
| 250 | + * @return mixed |
| 251 | + */ |
| 252 | + private function getProperty($name) |
| 253 | + { |
| 254 | + $property = $this->reflection->getProperty($name); |
| 255 | + $property->setAccessible(true); |
| 256 | + $value = $property->getValue($this->object); |
| 257 | + |
| 258 | + return $value; |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Call given object method. |
| 263 | + * |
| 264 | + * @param string $name |
| 265 | + * @param array[mixed] $arguments |
| 266 | + * @return mixed |
| 267 | + */ |
| 268 | + private function callMethod($name, $arguments = array()) |
| 269 | + { |
| 270 | + $method = $this->reflection->getMethod($name); |
| 271 | + $method->setAccessible(true); |
| 272 | + $result = $method->invokeArgs($this->object, $arguments); |
| 273 | + |
| 274 | + return $result; |
| 275 | + } |
| 276 | +} |
0 commit comments