You are here

public function ClassMethods::hydrate in Zircon Profile 8

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

Hydrate an object by populating getter/setter methods

Hydrates an object by getter/setter methods of the object.

Parameters

array $data:

object $object:

Return value

object

Throws

Exception\BadMethodCallException for a non-object $object

Overrides HydrationInterface::hydrate

File

vendor/zendframework/zend-hydrator/src/ClassMethods.php, line 191

Class

ClassMethods

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__));
  }
  $objectClass = get_class($object);
  foreach ($data as $property => $value) {
    $propertyFqn = $objectClass . '::$' . $property;
    if (!isset($this->hydrationMethodsCache[$propertyFqn])) {
      $setterName = 'set' . ucfirst($this
        ->hydrateName($property, $data));
      $this->hydrationMethodsCache[$propertyFqn] = is_callable([
        $object,
        $setterName,
      ]) ? $setterName : false;
    }
    if ($this->hydrationMethodsCache[$propertyFqn]) {
      $object
        ->{$this->hydrationMethodsCache[$propertyFqn]}($this
        ->hydrateValue($property, $value, $data));
    }
  }
  return $object;
}