Example #1
0
 /**
  * Extract values from an object with class methods
  *
  * Extracts the getter/setter of the given $object.
  *
  * @param  object                           $object
  * @return array
  * @throws Exception\BadMethodCallException for a non-object $object
  */
 public function extract($object)
 {
     if (!is_object($object)) {
         throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
     }
     $filter = null;
     if ($object instanceof FilterProviderInterface) {
         $filter = new FilterComposite(array($object->getFilter()), array(new MethodMatchFilter('getFilter')));
     } else {
         $filter = $this->filterComposite;
     }
     $attributes = array();
     $methods = get_class_methods($object);
     foreach ($methods as $method) {
         if (!$filter->filter(get_class($object) . '::' . $method)) {
             continue;
         }
         if (!$this->callableMethodFilter->filter(get_class($object) . '::' . $method)) {
             continue;
         }
         $attribute = $method;
         if (preg_match('/^get/', $method)) {
             $attribute = substr($method, 3);
             if (!property_exists($object, $attribute)) {
                 $attribute = lcfirst($attribute);
             }
         }
         $attribute = $this->extractName($attribute, $object);
         $attributes[$attribute] = $this->extractValue($attribute, $object->{$method}(), $object);
     }
     return $attributes;
 }
Example #2
0
 /**
  * (non-PHPdoc)
  * @see \Zend\Stdlib\Hydrator\ClassMethods::extract()
  */
 public function extract($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);
     // reset the hydrator's hydrator's cache for this object, as the filter may be per-instance
     if ($object instanceof FilterProviderInterface) {
         $this->extractionMethodsCache[$objectClass] = null;
     }
     // pass 1 - finding out which properties can be extracted, with which methods (populate hydration cache)
     if (!isset($this->extractionMethodsCache[$objectClass])) {
         $this->extractionMethodsCache[$objectClass] = array();
         $filter = $this->filterComposite;
         $methods = get_class_methods($object);
         if ($object instanceof FilterProviderInterface) {
             $filter = new FilterComposite(array($object->getFilter()), array(new MethodMatchFilter('getFilter')));
         }
         foreach ($methods as $method) {
             $methodFqn = $objectClass . '::' . $method;
             if (!($filter->filter($methodFqn) && $this->callableMethodFilter->filter($methodFqn))) {
                 continue;
             }
             $attribute = $method;
             if (strpos($method, 'get') === 0) {
                 $attribute = substr($method, 3);
                 if (!property_exists($object, $attribute)) {
                     $attribute = lcfirst($attribute);
                 }
             }
             $this->extractionMethodsCache[$objectClass][$method] = $attribute;
         }
     }
     #echo '<pre>';print_r($this->extractionMethodsCache[$objectClass]);echo '</pre>';exit;
     $values = array();
     // pass 2 - actually extract data
     foreach ($this->extractionMethodsCache[$objectClass] as $methodName => $attributeName) {
         $realAttributeName = $this->extractName($attributeName, $object);
         if (true === isset($this->fieldsetMap[$realAttributeName])) {
             $values[$this->fieldsetMap[$realAttributeName]][$realAttributeName] = $this->extractValue($realAttributeName, $object->{$methodName}(), $object);
         } else {
             $values[$realAttributeName] = $this->extractValue($realAttributeName, $object->{$methodName}(), $object);
         }
     }
     return $values;
 }
Example #3
0
 /**
  * Extract values from an object with class methods
  *
  * Extracts the getter/setter of the given $object.
  *
  * @param  object                           $object
  * @return array
  * @throws Exception\BadMethodCallException for a non-object $object
  */
 public function extract($object)
 {
     if (!is_object($object)) {
         throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
     }
     $filter = null;
     if ($object instanceof FilterProviderInterface) {
         $filter = new FilterComposite(array($object->getFilter()), array(new MethodMatchFilter("getFilter")));
     } else {
         $filter = $this->filterComposite;
     }
     $transform = function ($letters) {
         $letter = array_shift($letters);
         return '_' . strtolower($letter);
     };
     $attributes = array();
     $methods = get_class_methods($object);
     foreach ($methods as $method) {
         if (!$filter->filter(get_class($object) . '::' . $method)) {
             continue;
         }
         if (!$this->callableMethodFilter->filter(get_class($object) . '::' . $method)) {
             continue;
         }
         $attribute = $method;
         if (preg_match('/^get/', $method)) {
             $attribute = substr($method, 3);
             if (!property_exists($object, $attribute)) {
                 $attribute = lcfirst($attribute);
             }
         }
         if ($this->underscoreSeparatedKeys) {
             $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
         }
         $attributes[$attribute] = $this->extractValue($attribute, $object->{$method}(), $object);
     }
     return $attributes;
 }