Skip to content

Instantly share code, notes, and snippets.

@BinaryKitten
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save BinaryKitten/450576d9a570c4488429 to your computer and use it in GitHub Desktop.

Select an option

Save BinaryKitten/450576d9a570c4488429 to your computer and use it in GitHub Desktop.
<?php
/**
* Hydrate an object by populating public properties
*
* Hydrates an object by setting public properties of the object.
*
* @param array $data
* @param object $object
* @return 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__
));
}
$prop = &$this->propertyFilterCache[get_class($object)];
if (!isset($prop)) {
$prop = array_fill_keys(
array_filter(array_map(
function($property) {return trim(strrchr($property, "\0")); },
array_keys((array) $object)
)),
true);
}
foreach ($data as $name => $value) {
$property = $this->hydrateName($name, $data);
if (!empty($prop) && isset($prop[$property])) {
continue;
}
$object->$property = $this->hydrateValue($property, $value, $data);
}
return $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment