You are here

class ObjectProperty in Zircon Profile 8

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

Hierarchy

Expanded class hierarchy of ObjectProperty

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

File

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

Namespace

Zend\Hydrator
View source
class ObjectProperty extends AbstractHydrator {

  /**
   * @var array[] indexed by class name and then property name
   */
  private static $skippedPropertiesCache = [];

  /**
   * {@inheritDoc}
   *
   * Extracts the accessible non-static properties of the given $object.
   *
   * @throws Exception\BadMethodCallException for a non-object $object
   */
  public function extract($object) {
    if (!is_object($object)) {
      throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
    }
    $data = get_object_vars($object);
    $filter = $this
      ->getFilter();
    foreach ($data as $name => $value) {

      // Filter keys, removing any we don't want
      if (!$filter
        ->filter($name)) {
        unset($data[$name]);
        continue;
      }

      // Replace name if extracted differ
      $extracted = $this
        ->extractName($name, $object);
      if ($extracted !== $name) {
        unset($data[$name]);
        $name = $extracted;
      }
      $data[$name] = $this
        ->extractValue($name, $value, $object);
    }
    return $data;
  }

  /**
   * {@inheritDoc}
   *
   * Hydrate an object by populating public properties
   *
   * Hydrates an object by setting public properties of the object.
   *
   * @throws Exception\BadMethodCallException for a non-object $object
   */
  public function hydrate(array $data, $object) {
    if (!is_object($object)) {
      throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
    }
    $properties =& self::$skippedPropertiesCache[get_class($object)];
    if (!isset($properties)) {
      $reflection = new ReflectionClass($object);
      $properties = array_fill_keys(array_map(function (ReflectionProperty $property) {
        return $property
          ->getName();
      }, $reflection
        ->getProperties(ReflectionProperty::IS_PRIVATE + ReflectionProperty::IS_PROTECTED + ReflectionProperty::IS_STATIC)), true);
    }
    foreach ($data as $name => $value) {
      $property = $this
        ->hydrateName($name, $data);
      if (isset($properties[$property])) {
        continue;
      }
      $object->{$property} = $this
        ->hydrateValue($property, $value, $data);
    }
    return $object;
  }

}

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
ObjectProperty::$skippedPropertiesCache private static property
ObjectProperty::extract public function Extracts the accessible non-static properties of the given $object. Overrides ExtractionInterface::extract
ObjectProperty::hydrate public function Hydrate an object by populating public properties Overrides HydrationInterface::hydrate