You are here

protected function AbstractNormalizer::instantiateObject in Zircon Profile 8

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

Instantiates an object using constructor parameters when needed.

This method also allows to denormalize data into an existing object if it is present in the context with the object_to_populate key.

Parameters

array $data:

string $class:

array $context:

\ReflectionClass $reflectionClass:

array|bool $allowedAttributes:

Return value

object

Throws

RuntimeException

3 calls to AbstractNormalizer::instantiateObject()
GetSetMethodNormalizer::denormalize in vendor/symfony/serializer/Normalizer/GetSetMethodNormalizer.php
ObjectNormalizer::denormalize in vendor/symfony/serializer/Normalizer/ObjectNormalizer.php
Denormalizes data back into an object of the given class.
PropertyNormalizer::denormalize in vendor/symfony/serializer/Normalizer/PropertyNormalizer.php

File

vendor/symfony/serializer/Normalizer/AbstractNormalizer.php, line 294

Class

AbstractNormalizer
Normalizer implementation.

Namespace

Symfony\Component\Serializer\Normalizer

Code

protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes) {
  if (isset($context['object_to_populate']) && is_object($context['object_to_populate']) && $class === get_class($context['object_to_populate'])) {
    return $context['object_to_populate'];
  }
  $constructor = $reflectionClass
    ->getConstructor();
  if ($constructor) {
    $constructorParameters = $constructor
      ->getParameters();
    $params = array();
    foreach ($constructorParameters as $constructorParameter) {
      $paramName = $constructorParameter->name;
      $key = $this->nameConverter ? $this->nameConverter
        ->normalize($paramName) : $paramName;
      $allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
      $ignored = in_array($paramName, $this->ignoredAttributes);
      if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter
        ->isVariadic()) {
        if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
          if (!is_array($data[$paramName])) {
            throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
          }
          $params = array_merge($params, $data[$paramName]);
        }
      }
      elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
        $params[] = $data[$key];

        // don't run set for a parameter passed to the constructor
        unset($data[$key]);
      }
      elseif ($constructorParameter
        ->isDefaultValueAvailable()) {
        $params[] = $constructorParameter
          ->getDefaultValue();
      }
      else {
        throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
      }
    }
    return $reflectionClass
      ->newInstanceArgs($params);
  }
  return new $class();
}