Building an object to array hydrator
This recipe is the converse of the Creating an array to object hydrator recipe. In this case, we need to pull values from object properties and return an associative array where the key will be the column name.
How to do it...
For this illustration we will build upon the
Application\Generic\Hydrator\GetSet
class defined in the previous recipe:namespace Application\Generic\Hydrator; class GetSet { // code }
After the
hydrate()
method defined in the previous recipe, we define anextract()
method, which takes an object as an argument. The logic is similar to that used withhydrate()
, except this time we're searching forgetXXX()
methods. Again,preg_match()
is used to match the method prefix and its suffix, which is subsequently assumed to be the array key:public static function extract($object) { $array = array(); $class = get_class($object); $methodList = get_class_methods($class); foreach ($methodList as $method) { preg_match('/^(get)(.*?)...