You are here

public function PropertyNormalizer::denormalize 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::denormalize()

Throws

RuntimeException

Overrides DenormalizerInterface::denormalize

File

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

Class

PropertyNormalizer
Converts between objects and arrays by mapping properties.

Namespace

Symfony\Component\Serializer\Normalizer

Code

public function denormalize($data, $class, $format = null, array $context = array()) {
  $allowedAttributes = $this
    ->getAllowedAttributes($class, $context, true);
  $data = $this
    ->prepareForDenormalization($data);
  $reflectionClass = new \ReflectionClass($class);
  $object = $this
    ->instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes);
  foreach ($data as $propertyName => $value) {
    if ($this->nameConverter) {
      $propertyName = $this->nameConverter
        ->denormalize($propertyName);
    }
    $allowed = $allowedAttributes === false || in_array($propertyName, $allowedAttributes);
    $ignored = in_array($propertyName, $this->ignoredAttributes);
    if ($allowed && !$ignored && $reflectionClass
      ->hasProperty($propertyName)) {
      $property = $reflectionClass
        ->getProperty($propertyName);

      // Override visibility
      if (!$property
        ->isPublic()) {
        $property
          ->setAccessible(true);
      }
      $property
        ->setValue($object, $value);
    }
  }
  return $object;
}