Last active
August 29, 2015 14:09
-
-
Save BinaryKitten/450576d9a570c4488429 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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