You are here

public function PropertyNormalizer::normalize in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Normalizer/PropertyNormalizer.php \Symfony\Component\Serializer\Normalizer\PropertyNormalizer::normalize()

Throws

CircularReferenceException

Overrides NormalizerInterface::normalize

File

vendor/symfony/serializer/Normalizer/PropertyNormalizer.php, line 42

Class

PropertyNormalizer
Converts between objects and arrays by mapping properties.

Namespace

Symfony\Component\Serializer\Normalizer

Code

public function normalize($object, $format = null, array $context = array()) {
  if ($this
    ->isCircularReference($object, $context)) {
    return $this
      ->handleCircularReference($object);
  }
  $reflectionObject = new \ReflectionObject($object);
  $attributes = array();
  $allowedAttributes = $this
    ->getAllowedAttributes($object, $context, true);
  foreach ($reflectionObject
    ->getProperties() as $property) {
    if (in_array($property->name, $this->ignoredAttributes)) {
      continue;
    }
    if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) {
      continue;
    }

    // Override visibility
    if (!$property
      ->isPublic()) {
      $property
        ->setAccessible(true);
    }
    $attributeValue = $property
      ->getValue($object);
    if (isset($this->callbacks[$property->name])) {
      $attributeValue = call_user_func($this->callbacks[$property->name], $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', $property->name));
      }
      $attributeValue = $this->serializer
        ->normalize($attributeValue, $format, $context);
    }
    $propertyName = $property->name;
    if ($this->nameConverter) {
      $propertyName = $this->nameConverter
        ->normalize($propertyName);
    }
    $attributes[$propertyName] = $attributeValue;
  }
  return $attributes;
}