You are here

class Reflection in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-hydrator/src/Reflection.php \Zend\Hydrator\Reflection
  2. 8 vendor/zendframework/zend-stdlib/src/Hydrator/Reflection.php \Zend\Stdlib\Hydrator\Reflection
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-hydrator/src/Reflection.php \Zend\Hydrator\Reflection

Hierarchy

Expanded class hierarchy of Reflection

1 file declares its use of Reflection
Reflection.php in vendor/zendframework/zend-stdlib/src/Hydrator/Reflection.php

File

vendor/zendframework/zend-hydrator/src/Reflection.php, line 15

Namespace

Zend\Hydrator
View source
class Reflection extends AbstractHydrator {

  /**
   * Simple in-memory array cache of ReflectionProperties used.
   * @var ReflectionProperty[]
   */
  protected static $reflProperties = [];

  /**
   * Extract values from an object
   *
   * @param  object $object
   * @return array
   */
  public function extract($object) {
    $result = [];
    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;
  }

  /**
   * Hydrate $object with the provided $data.
   *
   * @param  array $data
   * @param  object $object
   * @return object
   */
  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;
  }

  /**
   * Get a reflection properties from in-memory cache and lazy-load if
   * class has not been loaded.
   *
   * @param  string|object $input
   * @throws Exception\InvalidArgumentException
   * @return ReflectionProperty[]
   */
  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] = [];
    $reflClass = new ReflectionClass($input);
    $reflProperties = $reflClass
      ->getProperties();
    foreach ($reflProperties as $property) {
      $property
        ->setAccessible(true);
      static::$reflProperties[$input][$property
        ->getName()] = $property;
    }
    return static::$reflProperties[$input];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractHydrator::$filterComposite protected property Composite to filter the methods, that need to be hydrated
AbstractHydrator::$namingStrategy protected property An instance of NamingStrategy\NamingStrategyInterface
AbstractHydrator::$strategies protected property The list with strategies that this hydrator has.
AbstractHydrator::addFilter public function Add a new filter to take care of what needs to be hydrated. To exclude e.g. the method getServiceLocator: Overrides FilterEnabledInterface::addFilter 1
AbstractHydrator::addStrategy public function Adds the given strategy under the given name. Overrides StrategyEnabledInterface::addStrategy
AbstractHydrator::extractName public function Convert a name for extraction. If no naming strategy exists, the plain value is returned.
AbstractHydrator::extractValue public function Converts a value for extraction. If no strategy exists the plain value is returned.
AbstractHydrator::getFilter public function Get the filter instance
AbstractHydrator::getNamingStrategy public function Gets the naming strategy. Overrides NamingStrategyEnabledInterface::getNamingStrategy
AbstractHydrator::getStrategy public function Gets the strategy with the given name. Overrides StrategyEnabledInterface::getStrategy
AbstractHydrator::hasFilter public function Check whether a specific filter exists at key $name or not Overrides FilterEnabledInterface::hasFilter
AbstractHydrator::hasNamingStrategy public function Checks if a naming strategy exists. Overrides NamingStrategyEnabledInterface::hasNamingStrategy
AbstractHydrator::hasStrategy public function Checks if the strategy with the given name exists. Overrides StrategyEnabledInterface::hasStrategy
AbstractHydrator::hydrateName public function Converts a value for hydration. If no naming strategy exists, the plain value is returned.
AbstractHydrator::hydrateValue public function Converts a value for hydration. If no strategy exists the plain value is returned.
AbstractHydrator::removeFilter public function Remove a filter from the composition. To not extract "has" methods, you simply need to unregister it Overrides FilterEnabledInterface::removeFilter 1
AbstractHydrator::removeNamingStrategy public function Removes the naming strategy Overrides NamingStrategyEnabledInterface::removeNamingStrategy 1
AbstractHydrator::removeStrategy public function Removes the strategy with the given name. Overrides StrategyEnabledInterface::removeStrategy
AbstractHydrator::setNamingStrategy public function Adds the given naming strategy Overrides NamingStrategyEnabledInterface::setNamingStrategy 1
AbstractHydrator::__construct public function Initializes a new instance of this class. 1
Reflection::$reflProperties protected static property Simple in-memory array cache of ReflectionProperties used.
Reflection::extract public function Extract values from an object Overrides ExtractionInterface::extract
Reflection::getReflProperties protected static function Get a reflection properties from in-memory cache and lazy-load if class has not been loaded.
Reflection::hydrate public function Hydrate $object with the provided $data. Overrides HydrationInterface::hydrate