public function GetSetMethodNormalizer::denormalize

Denormalizes data back into an object of the given class

Parameters

mixed $data data to restore:

string $class the expected class to instantiate:

string $format format the given data was extracted from:

array $context options available to the denormalizer:

Return value

object

Overrides DenormalizerInterface::denormalize

File

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

Class

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

Namespace

Symfony\Component\Serializer\Normalizer

Code

public function denormalize($data, $class, $format = null, array $context = array()) {
  $reflectionClass = new \ReflectionClass($class);
  $constructor = $reflectionClass
    ->getConstructor();
  if ($constructor) {
    $constructorParameters = $constructor
      ->getParameters();
    $params = array();
    foreach ($constructorParameters as $constructorParameter) {
      $paramName = lcfirst($this
        ->formatAttribute($constructorParameter->name));
      if (isset($data[$paramName])) {
        $params[] = $data[$paramName];

        // don't run set for a parameter passed to the constructor
        unset($data[$paramName]);
      }
      elseif (!$constructorParameter
        ->isOptional()) {
        throw new RuntimeException('Cannot create an instance of ' . $class . ' from serialized data because its constructor requires ' . 'parameter "' . $constructorParameter->name . '" to be present.');
      }
    }
    $object = $reflectionClass
      ->newInstanceArgs($params);
  }
  else {
    $object = new $class();
  }
  foreach ($data as $attribute => $value) {
    $setter = 'set' . $this
      ->formatAttribute($attribute);
    if (method_exists($object, $setter)) {
      $object
        ->{$setter}($value);
    }
  }
  return $object;
}