Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4412ab4

Browse filesBrowse files
committed
[Serializer] add method for preparing constructor argument in AbstractNormalizer
1 parent 9ef362e commit 4412ab4
Copy full SHA for 4412ab4

File tree

1 file changed

+44
-27
lines changed
Filter options

1 file changed

+44
-27
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+44-27Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -361,33 +361,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
361361
$params = array_merge($params, $data[$paramName]);
362362
}
363363
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
364-
$parameterData = $data[$key];
365-
if (null === $parameterData && $constructorParameter->allowsNull()) {
366-
$params[] = null;
367-
// Don't run set for a parameter passed to the constructor
368-
unset($data[$key]);
369-
continue;
370-
}
371-
try {
372-
if (null !== $constructorParameter->getClass()) {
373-
if (!$this->serializer instanceof DenormalizerInterface) {
374-
throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $constructorParameter->getClass(), static::class));
375-
}
376-
$parameterClass = $constructorParameter->getClass()->getName();
377-
$parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName));
378-
}
379-
} catch (\ReflectionException $e) {
380-
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
381-
} catch (MissingConstructorArgumentsException $e) {
382-
if (!$constructorParameter->getType()->allowsNull()) {
383-
throw $e;
384-
}
385-
$parameterData = null;
386-
}
387-
388-
// Don't run set for a parameter passed to the constructor
389-
$params[] = $parameterData;
390-
unset($data[$key]);
364+
$params[] = $this->createConstructorArgument($data, $key, $constructorParameter, $context, $format);
391365
} elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
392366
$params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
393367
} elseif ($constructorParameter->isDefaultValueAvailable()) {
@@ -413,6 +387,49 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
413387
return new $class();
414388
}
415389

390+
/**
391+
* @param array $data
392+
* @param string $key
393+
* @param \ReflectionParameter $constructorParameter
394+
* @param array $context
395+
* @param string|null $format
396+
*
397+
* @return mixed value of constructor parameter - an argument
398+
*
399+
* @throws LogicException
400+
* @throws RuntimeException
401+
* @throws MissingConstructorArgumentsException
402+
*/
403+
protected function createConstructorArgument(array &$data, string $key, \ReflectionParameter $constructorParameter, array &$context, string $format = null)
404+
{
405+
$parameterData = $data[$key];
406+
if (null === $parameterData && $constructorParameter->allowsNull()) {
407+
// Don't run set for a parameter passed to the constructor
408+
unset($data[$key]);
409+
return null;
410+
}
411+
try {
412+
if (null !== $constructorParameter->getClass()) {
413+
if (!$this->serializer instanceof DenormalizerInterface) {
414+
throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $constructorParameter->getClass(), static::class));
415+
}
416+
$parameterClass = $constructorParameter->getClass()->getName();
417+
$parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $constructorParameter->name));
418+
}
419+
} catch (\ReflectionException $e) {
420+
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
421+
} catch (MissingConstructorArgumentsException $e) {
422+
if (!$constructorParameter->getType()->allowsNull()) {
423+
throw $e;
424+
}
425+
$parameterData = null;
426+
}
427+
428+
// Don't run set for a parameter passed to the constructor
429+
unset($data[$key]);
430+
return $parameterData;
431+
}
432+
416433
/**
417434
* @param array $parentContext
418435
* @param string $attribute

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.