public function GetSetMethodNormalizer::normalize

Normalizes an object into a set of arrays/scalars

Parameters

object $object object to normalize:

string $format format the normalization result will be encoded as:

array $context Context options for the normalizer:

Return value

array|scalar

Overrides NormalizerInterface::normalize

File

drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php, line 83

Class

GetSetMethodNormalizer
Converts between objects with getter and setter methods and arrays.

Namespace

Symfony\Component\Serializer\Normalizer

Code

public function normalize($object, $format = null, array $context = array()) {
  $reflectionObject = new \ReflectionObject($object);
  $reflectionMethods = $reflectionObject
    ->getMethods(\ReflectionMethod::IS_PUBLIC);
  $attributes = array();
  foreach ($reflectionMethods as $method) {
    if ($this
      ->isGetMethod($method)) {
      $attributeName = lcfirst(substr($method->name, 3));
      if (in_array($attributeName, $this->ignoredAttributes)) {
        continue;
      }
      $attributeValue = $method
        ->invoke($object);
      if (array_key_exists($attributeName, $this->callbacks)) {
        $attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
      }
      if (null !== $attributeValue && !is_scalar($attributeValue)) {
        $attributeValue = $this->serializer
          ->normalize($attributeValue, $format);
      }
      $attributes[$attributeName] = $attributeValue;
    }
  }
  return $attributes;
}