3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib\Hydrator
;
13 use Zend\Stdlib\Exception
;
15 class Reflection
extends AbstractHydrator
18 * Simple in-memory array cache of ReflectionProperties used.
21 protected static $reflProperties = array();
24 * Extract values from an object
26 * @param object $object
29 public function extract($object)
32 foreach (self
::getReflProperties($object) as $property) {
33 $propertyName = $property->getName();
34 if (!$this->filterComposite
->filter($propertyName)) {
38 $value = $property->getValue($object);
39 $result[$propertyName] = $this->extractValue($propertyName, $value, $object);
46 * Hydrate $object with the provided $data.
49 * @param object $object
52 public function hydrate(array $data, $object)
54 $reflProperties = self
::getReflProperties($object);
55 foreach ($data as $key => $value) {
56 if (isset($reflProperties[$key])) {
57 $reflProperties[$key]->setValue($object, $this->hydrateValue($key, $value, $data));
64 * Get a reflection properties from in-memory cache and lazy-load if
65 * class has not been loaded.
67 * @param string|object $input
68 * @throws Exception\InvalidArgumentException
71 protected static function getReflProperties($input)
73 if (is_object($input)) {
74 $input = get_class($input);
75 } elseif (!is_string($input)) {
76 throw new Exception\
InvalidArgumentException('Input must be a string or an object.');
79 if (isset(static::$reflProperties[$input])) {
80 return static::$reflProperties[$input];
83 static::$reflProperties[$input] = array();
84 $reflClass = new ReflectionClass($input);
85 $reflProperties = $reflClass->getProperties();
87 foreach ($reflProperties as $property) {
88 $property->setAccessible(true);
89 static::$reflProperties[$input][$property->getName()] = $property;
92 return static::$reflProperties[$input];