You are here

public function ObjectProperty::hydrate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-hydrator/src/ObjectProperty.php \Zend\Hydrator\ObjectProperty::hydrate()

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

Overrides HydrationInterface::hydrate

File

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

Class

ObjectProperty

Namespace

Zend\Hydrator

Code

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;
}