Denormalizes data back into an object of the given class
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:
object
Overrides DenormalizerInterface::denormalize
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;
}