You are here

public function ClassMethods::extract in Zircon Profile 8

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

Extract values from an object with class methods

Extracts the getter/setter of the given $object.

Parameters

object $object:

Return value

array

Throws

Exception\BadMethodCallException for a non-object $object

Overrides ExtractionInterface::extract

File

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

Class

ClassMethods

Namespace

Zend\Hydrator

Code

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 Filter\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] = [];
    $filter = $this->filterComposite;
    $methods = get_class_methods($object);
    if ($object instanceof Filter\FilterProviderInterface) {
      $filter = new Filter\FilterComposite([
        $object
          ->getFilter(),
      ], [
        new Filter\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;
    }
  }
  $values = [];

  // pass 2 - actually extract data
  foreach ($this->extractionMethodsCache[$objectClass] as $methodName => $attributeName) {
    $realAttributeName = $this
      ->extractName($attributeName, $object);
    $values[$realAttributeName] = $this
      ->extractValue($realAttributeName, $object
      ->{$methodName}(), $object);
  }
  return $values;
}