1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace Zend\Stdlib\Hydrator;
- use ReflectionClass;
- use Zend\Stdlib\Exception;
- class Reflection extends AbstractHydrator
- {
-
- protected static $reflProperties = array();
-
- public function extract($object)
- {
- $result = array();
- foreach (self::getReflProperties($object) as $property) {
- $propertyName = $this->extractName($property->getName(), $object);
- if (!$this->filterComposite->filter($propertyName)) {
- continue;
- }
- $value = $property->getValue($object);
- $result[$propertyName] = $this->extractValue($propertyName, $value, $object);
- }
- return $result;
- }
-
- public function hydrate(array $data, $object)
- {
- $reflProperties = self::getReflProperties($object);
- foreach ($data as $key => $value) {
- $name = $this->hydrateName($key, $data);
- if (isset($reflProperties[$name])) {
- $reflProperties[$name]->setValue($object, $this->hydrateValue($name, $value, $data));
- }
- }
- return $object;
- }
-
- protected static function getReflProperties($input)
- {
- if (is_object($input)) {
- $input = get_class($input);
- } elseif (!is_string($input)) {
- throw new Exception\InvalidArgumentException('Input must be a string or an object.');
- }
- if (isset(static::$reflProperties[$input])) {
- return static::$reflProperties[$input];
- }
- static::$reflProperties[$input] = array();
- $reflClass = new ReflectionClass($input);
- $reflProperties = $reflClass->getProperties();
- foreach ($reflProperties as $property) {
- $property->setAccessible(true);
- static::$reflProperties[$input][$property->getName()] = $property;
- }
- return static::$reflProperties[$input];
- }
- }
|