From 0475d7e754f50746f88fb9f93e4e44c4c0b0aa75 Mon Sep 17 00:00:00 2001 From: Konrad Mikucki Date: Fri, 24 Aug 2018 09:25:32 +0200 Subject: [PATCH 1/2] [Serializer] add method for preparing constructor argument in AbstractNormalizer --- .../Normalizer/AbstractNormalizer.php | 73 ++++++++++++------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 78102cb6a9616..1c84910d39646 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -361,33 +361,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref $params = array_merge($params, $data[$paramName]); } } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { - $parameterData = $data[$key]; - if (null === $parameterData && $constructorParameter->allowsNull()) { - $params[] = null; - // Don't run set for a parameter passed to the constructor - unset($data[$key]); - continue; - } - try { - if (null !== $constructorParameter->getClass()) { - if (!$this->serializer instanceof DenormalizerInterface) { - 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)); - } - $parameterClass = $constructorParameter->getClass()->getName(); - $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName)); - } - } catch (\ReflectionException $e) { - throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); - } catch (MissingConstructorArgumentsException $e) { - if (!$constructorParameter->getType()->allowsNull()) { - throw $e; - } - $parameterData = null; - } - - // Don't run set for a parameter passed to the constructor - $params[] = $parameterData; - unset($data[$key]); + $params[] = $this->createConstructorArgument($data, $key, $constructorParameter, $context, $format); } elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) { $params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key]; } elseif ($constructorParameter->isDefaultValueAvailable()) { @@ -407,6 +381,51 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref return new $class(); } + /** + * @param array $data + * @param string $key + * @param \ReflectionParameter $constructorParameter + * @param array $context + * @param string|null $format + * + * @return mixed value of constructor parameter - an argument + * + * @throws LogicException + * @throws RuntimeException + * @throws MissingConstructorArgumentsException + */ + protected function createConstructorArgument(array &$data, string $key, \ReflectionParameter $constructorParameter, array &$context, string $format = null) + { + $parameterData = $data[$key]; + if (null === $parameterData && $constructorParameter->allowsNull()) { + // Don't run set for a parameter passed to the constructor + unset($data[$key]); + + return null; + } + try { + if (null !== $constructorParameter->getClass()) { + if (!$this->serializer instanceof DenormalizerInterface) { + 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)); + } + $parameterClass = $constructorParameter->getClass()->getName(); + $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $constructorParameter->name)); + } + } catch (\ReflectionException $e) { + throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); + } catch (MissingConstructorArgumentsException $e) { + if (!$constructorParameter->getType()->allowsNull()) { + throw $e; + } + $parameterData = null; + } + + // Don't run set for a parameter passed to the constructor + unset($data[$key]); + + return $parameterData; + } + /** * @param array $parentContext * @param string $attribute From 2c37a8b84334146742ad9c3a7148fdf5debc2f83 Mon Sep 17 00:00:00 2001 From: Konrad Mikucki Date: Sun, 26 Aug 2018 16:05:52 +0200 Subject: [PATCH 2/2] remove data parameter from createConstructorArgument method --- .../Normalizer/AbstractNormalizer.php | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 1c84910d39646..35fc1fc3b69b3 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -361,7 +361,10 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref $params = array_merge($params, $data[$paramName]); } } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { - $params[] = $this->createConstructorArgument($data, $key, $constructorParameter, $context, $format); + $params[] = $this->createConstructorArgument($data[$key], $key, $constructorParameter, $context, $format); + + // Don't run set for a parameter passed to the constructor + unset($data[$key]); } elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) { $params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key]; } elseif ($constructorParameter->isDefaultValueAvailable()) { @@ -382,7 +385,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref } /** - * @param array $data + * @param mixed $parameterData * @param string $key * @param \ReflectionParameter $constructorParameter * @param array $context @@ -394,13 +397,9 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref * @throws RuntimeException * @throws MissingConstructorArgumentsException */ - protected function createConstructorArgument(array &$data, string $key, \ReflectionParameter $constructorParameter, array &$context, string $format = null) + protected function createConstructorArgument($parameterData, string $key, \ReflectionParameter $constructorParameter, array &$context, string $format = null) { - $parameterData = $data[$key]; if (null === $parameterData && $constructorParameter->allowsNull()) { - // Don't run set for a parameter passed to the constructor - unset($data[$key]); - return null; } try { @@ -409,7 +408,8 @@ protected function createConstructorArgument(array &$data, string $key, \Reflect 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)); } $parameterClass = $constructorParameter->getClass()->getName(); - $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $constructorParameter->name)); + + return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $constructorParameter->name)); } } catch (\ReflectionException $e) { throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e); @@ -417,11 +417,9 @@ protected function createConstructorArgument(array &$data, string $key, \Reflect if (!$constructorParameter->getType()->allowsNull()) { throw $e; } - $parameterData = null; - } - // Don't run set for a parameter passed to the constructor - unset($data[$key]); + return null; + } return $parameterData; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy