Address comments
This commit is contained in:
parent
e99a90b9e9
commit
e64e999a39
@ -268,6 +268,15 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
return $reflectionClass->getConstructor();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see instantiateComplexObject
|
||||
* @deprecated Since 3.1, will be removed in 4.0. Use instantiateComplexObject instead.
|
||||
*/
|
||||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
||||
{
|
||||
return $this->instantiateComplexObject($data, $class, $context, $reflectionClass, $allowedAttributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates an object using constructor parameters when needed.
|
||||
*
|
||||
@ -287,7 +296,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
|
||||
protected function instantiateComplexObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
|
||||
{
|
||||
if (
|
||||
isset($context[static::OBJECT_TO_POPULATE]) &&
|
||||
@ -319,33 +328,41 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
|
||||
$params = array_merge($params, $data[$paramName]);
|
||||
}
|
||||
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
||||
$parameterData = $data[$key];
|
||||
if (null !== $constructorParameter->getClass()) {
|
||||
$parameterData = $this->serializer->denormalize($parameterData, $constructorParameter->getClass()->getName(), null, $context);
|
||||
$parameterData = $this->serializer->deserialize($parameterData, $constructorParameter->getClass()->getName(), null, $context);
|
||||
}
|
||||
|
||||
// Don't run set for a parameter passed to the constructor
|
||||
$params[] = $parameterData;
|
||||
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
|
||||
)
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($constructorParameter->isDefaultValueAvailable()) {
|
||||
$params[] = $constructorParameter->getDefaultValue();
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($constructor->isConstructor()) {
|
||||
return $reflectionClass->newInstanceArgs($params);
|
||||
} else {
|
||||
return $constructor->invokeArgs(null, $params);
|
||||
}
|
||||
|
||||
return $constructor->invokeArgs(null, $params);
|
||||
}
|
||||
|
||||
return new $class();
|
||||
|
@ -175,7 +175,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
||||
$normalizedData = $this->prepareForDenormalization($data);
|
||||
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
|
||||
$object = $this->instantiateComplexObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
|
||||
|
||||
foreach ($normalizedData as $attribute => $value) {
|
||||
if ($this->nameConverter) {
|
||||
|
@ -47,7 +47,7 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
|
||||
$normalizedData = $this->prepareForDenormalization($data);
|
||||
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
|
||||
$object = $this->instantiateComplexObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
|
||||
|
||||
$classMethods = get_class_methods($object);
|
||||
foreach ($normalizedData as $attribute => $value) {
|
||||
|
@ -159,13 +159,13 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testConstructorWithObjectTypeHintDenormalize()
|
||||
{
|
||||
$data = [
|
||||
$data = array(
|
||||
'id' => 10,
|
||||
'inner' => [
|
||||
'inner' => array(
|
||||
'foo' => 'oof',
|
||||
'bar' => 'rab',
|
||||
],
|
||||
];
|
||||
),
|
||||
);
|
||||
|
||||
$obj = $this->normalizer->denormalize($data, DummyWithConstructorObject::class);
|
||||
$this->assertInstanceOf(DummyWithConstructorObject::class, $obj);
|
||||
|
Reference in New Issue
Block a user