Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e64e999

Browse files
committed
Address comments
1 parent e99a90b commit e64e999

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
268268
return $reflectionClass->getConstructor();
269269
}
270270

271+
/**
272+
* @see instantiateComplexObject
273+
* @deprecated Since 3.1, will be removed in 4.0. Use instantiateComplexObject instead.
274+
*/
275+
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
276+
{
277+
return $this->instantiateComplexObject($data, $class, $context, $reflectionClass, $allowedAttributes);
278+
}
279+
271280
/**
272281
* Instantiates an object using constructor parameters when needed.
273282
*
@@ -287,7 +296,7 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
287296
*
288297
* @throws RuntimeException
289298
*/
290-
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
299+
protected function instantiateComplexObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
291300
{
292301
if (
293302
isset($context[static::OBJECT_TO_POPULATE]) &&
@@ -319,33 +328,41 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
319328

320329
$params = array_merge($params, $data[$paramName]);
321330
}
322-
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
331+
332+
continue;
333+
}
334+
335+
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
323336
$parameterData = $data[$key];
324337
if (null !== $constructorParameter->getClass()) {
325-
$parameterData = $this->serializer->denormalize($parameterData, $constructorParameter->getClass()->getName(), null, $context);
338+
$parameterData = $this->serializer->deserialize($parameterData, $constructorParameter->getClass()->getName(), null, $context);
326339
}
327340

328341
// Don't run set for a parameter passed to the constructor
329342
$params[] = $parameterData;
330343
unset($data[$key]);
331-
} elseif ($constructorParameter->isDefaultValueAvailable()) {
344+
345+
continue;
346+
}
347+
348+
if ($constructorParameter->isDefaultValueAvailable()) {
332349
$params[] = $constructorParameter->getDefaultValue();
333-
} else {
334-
throw new RuntimeException(
335-
sprintf(
336-
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
337-
$class,
338-
$constructorParameter->name
339-
)
340-
);
341350
}
351+
352+
throw new RuntimeException(
353+
sprintf(
354+
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
355+
$class,
356+
$constructorParameter->name
357+
)
358+
);
342359
}
343360

344361
if ($constructor->isConstructor()) {
345362
return $reflectionClass->newInstanceArgs($params);
346-
} else {
347-
return $constructor->invokeArgs(null, $params);
348363
}
364+
365+
return $constructor->invokeArgs(null, $params);
349366
}
350367

351368
return new $class();

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
175175
$normalizedData = $this->prepareForDenormalization($data);
176176

177177
$reflectionClass = new \ReflectionClass($class);
178-
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
178+
$object = $this->instantiateComplexObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
179179

180180
foreach ($normalizedData as $attribute => $value) {
181181
if ($this->nameConverter) {

src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
4747
$normalizedData = $this->prepareForDenormalization($data);
4848

4949
$reflectionClass = new \ReflectionClass($class);
50-
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
50+
$object = $this->instantiateComplexObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
5151

5252
$classMethods = get_class_methods($object);
5353
foreach ($normalizedData as $attribute => $value) {

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ public function testConstructorWithObjectDenormalize()
159159

160160
public function testConstructorWithObjectTypeHintDenormalize()
161161
{
162-
$data = [
162+
$data = array(
163163
'id' => 10,
164-
'inner' => [
164+
'inner' => array(
165165
'foo' => 'oof',
166166
'bar' => 'rab',
167-
],
168-
];
167+
),
168+
);
169169

170170
$obj = $this->normalizer->denormalize($data, DummyWithConstructorObject::class);
171171
$this->assertInstanceOf(DummyWithConstructorObject::class, $obj);

0 commit comments

Comments
 (0)