You are here

class ObjectNormalizer in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Normalizer/ObjectNormalizer.php \Symfony\Component\Serializer\Normalizer\ObjectNormalizer

Converts between objects and arrays using the PropertyAccess component.

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

Expanded class hierarchy of ObjectNormalizer

1 file declares its use of ObjectNormalizer
ObjectNormalizerTest.php in vendor/symfony/serializer/Tests/Normalizer/ObjectNormalizerTest.php

File

vendor/symfony/serializer/Normalizer/ObjectNormalizer.php, line 27

Namespace

Symfony\Component\Serializer\Normalizer
View source
class ObjectNormalizer extends AbstractNormalizer {

  /**
   * @var PropertyAccessorInterface
   */
  protected $propertyAccessor;
  public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null) {
    parent::__construct($classMetadataFactory, $nameConverter);
    $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
  }

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = null) {
    return is_object($data) && !$data instanceof \Traversable;
  }

  /**
   * {@inheritdoc}
   *
   * @throws CircularReferenceException
   */
  public function normalize($object, $format = null, array $context = array()) {
    if ($this
      ->isCircularReference($object, $context)) {
      return $this
        ->handleCircularReference($object);
    }
    $data = array();
    $attributes = $this
      ->getAllowedAttributes($object, $context, true);

    // If not using groups, detect manually
    if (false === $attributes) {
      $attributes = array();

      // methods
      $reflClass = new \ReflectionClass($object);
      foreach ($reflClass
        ->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
        if (!$reflMethod
          ->isConstructor() && !$reflMethod
          ->isDestructor() && 0 === $reflMethod
          ->getNumberOfRequiredParameters()) {
          $name = $reflMethod
            ->getName();
          if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {

            // getters and hassers
            $attributes[lcfirst(substr($name, 3))] = true;
          }
          elseif (strpos($name, 'is') === 0) {

            // issers
            $attributes[lcfirst(substr($name, 2))] = true;
          }
        }
      }

      // properties
      foreach ($reflClass
        ->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
        $attributes[$reflProperty
          ->getName()] = true;
      }
      $attributes = array_keys($attributes);
    }
    foreach ($attributes as $attribute) {
      if (in_array($attribute, $this->ignoredAttributes)) {
        continue;
      }
      $attributeValue = $this->propertyAccessor
        ->getValue($object, $attribute);
      if (isset($this->callbacks[$attribute])) {
        $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
      }
      if (null !== $attributeValue && !is_scalar($attributeValue)) {
        if (!$this->serializer instanceof NormalizerInterface) {
          throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute));
        }
        $attributeValue = $this->serializer
          ->normalize($attributeValue, $format, $context);
      }
      if ($this->nameConverter) {
        $attribute = $this->nameConverter
          ->normalize($attribute);
      }
      $data[$attribute] = $attributeValue;
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = null) {
    return class_exists($type);
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = null, array $context = array()) {
    $allowedAttributes = $this
      ->getAllowedAttributes($class, $context, true);
    $normalizedData = $this
      ->prepareForDenormalization($data);
    $reflectionClass = new \ReflectionClass($class);
    $object = $this
      ->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
    foreach ($normalizedData as $attribute => $value) {
      if ($this->nameConverter) {
        $attribute = $this->nameConverter
          ->denormalize($attribute);
      }
      $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
      $ignored = in_array($attribute, $this->ignoredAttributes);
      if ($allowed && !$ignored) {
        try {
          $this->propertyAccessor
            ->setValue($object, $attribute, $value);
        } catch (NoSuchPropertyException $exception) {

          // Properties not found are ignored
        }
      }
    }
    return $object;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractNormalizer::$callbacks protected property
AbstractNormalizer::$camelizedAttributes protected property
AbstractNormalizer::$circularReferenceHandler protected property
AbstractNormalizer::$circularReferenceLimit protected property
AbstractNormalizer::$classMetadataFactory protected property
AbstractNormalizer::$ignoredAttributes protected property
AbstractNormalizer::$nameConverter protected property
AbstractNormalizer::formatAttribute Deprecated protected function Format an attribute name, for example to convert a snake_case name to camelCase.
AbstractNormalizer::getAllowedAttributes protected function Gets attributes to normalize using groups.
AbstractNormalizer::handleCircularReference protected function Handles a circular reference.
AbstractNormalizer::instantiateObject protected function Instantiates an object using constructor parameters when needed.
AbstractNormalizer::isCircularReference protected function Detects if the configured circular reference limit is reached.
AbstractNormalizer::prepareForDenormalization protected function Normalizes the given data to an array. It's particularly useful during the denormalization process.
AbstractNormalizer::setCallbacks public function Set normalization callbacks.
AbstractNormalizer::setCamelizedAttributes Deprecated public function Set attributes to be camelized on denormalize.
AbstractNormalizer::setCircularReferenceHandler public function Set circular reference handler.
AbstractNormalizer::setCircularReferenceLimit public function Set circular reference limit.
AbstractNormalizer::setIgnoredAttributes public function Set ignored attributes for normalization and denormalization.
ObjectNormalizer::$propertyAccessor protected property
ObjectNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides DenormalizerInterface::denormalize
ObjectNormalizer::normalize public function Overrides NormalizerInterface::normalize
ObjectNormalizer::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer. Overrides DenormalizerInterface::supportsDenormalization
ObjectNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerInterface::supportsNormalization
ObjectNormalizer::__construct public function Sets the {@link ClassMetadataFactoryInterface} to use. Overrides AbstractNormalizer::__construct
SerializerAwareNormalizer::$serializer protected property
SerializerAwareNormalizer::setSerializer public function Sets the owning Serializer object. Overrides SerializerAwareInterface::setSerializer