Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Stdlib / Hydrator / Reflection.php
blobfae361aab78b629bd68eb089b4b159b15a5d8125
1 <?php
2 /**
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
8 */
10 namespace Zend\Stdlib\Hydrator;
12 use ReflectionClass;
13 use Zend\Stdlib\Exception;
15 class Reflection extends AbstractHydrator
17 /**
18 * Simple in-memory array cache of ReflectionProperties used.
19 * @var array
21 protected static $reflProperties = array();
23 /**
24 * Extract values from an object
26 * @param object $object
27 * @return array
29 public function extract($object)
31 $result = array();
32 foreach (self::getReflProperties($object) as $property) {
33 $propertyName = $property->getName();
34 if (!$this->filterComposite->filter($propertyName)) {
35 continue;
38 $value = $property->getValue($object);
39 $result[$propertyName] = $this->extractValue($propertyName, $value, $object);
42 return $result;
45 /**
46 * Hydrate $object with the provided $data.
48 * @param array $data
49 * @param object $object
50 * @return 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));
60 return $object;
63 /**
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
69 * @return array
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];