Mercurial > hg > isophonics-drupal-site
diff vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 7a779792577d |
children | c2387f117808 |
line wrap: on
line diff
--- a/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php Mon Apr 23 09:33:26 2018 +0100 +++ b/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php Mon Apr 23 09:46:53 2018 +0100 @@ -13,8 +13,9 @@ use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Exception\ExtraAttributesException; use Symfony\Component\Serializer\Exception\LogicException; -use Symfony\Component\Serializer\Exception\UnexpectedValueException; +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; @@ -30,9 +31,11 @@ { const ENABLE_MAX_DEPTH = 'enable_max_depth'; const DEPTH_KEY_PATTERN = 'depth_%s::%s'; + const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement'; private $propertyTypeExtractor; private $attributesCache = array(); + private $cache = array(); public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null) { @@ -46,7 +49,7 @@ */ public function supportsNormalization($data, $format = null) { - return is_object($data) && !$data instanceof \Traversable; + return \is_object($data) && !$data instanceof \Traversable; } /** @@ -91,7 +94,7 @@ throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute)); } - $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $context)); + $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute))); } return $data; @@ -125,6 +128,10 @@ return $allowedAttributes; } + if (isset($context['attributes'])) { + return $this->extractAttributes($object, $format, $context); + } + if (isset($this->attributesCache[$class])) { return $this->attributesCache[$class]; } @@ -160,7 +167,7 @@ */ public function supportsDenormalization($data, $type, $format = null) { - return class_exists($type); + return isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = class_exists($type); } /** @@ -171,8 +178,10 @@ if (!isset($context['cache_key'])) { $context['cache_key'] = $this->getCacheKey($format, $context); } + $allowedAttributes = $this->getAllowedAttributes($class, $context, true); $normalizedData = $this->prepareForDenormalization($data); + $extraAttributes = array(); $reflectionClass = new \ReflectionClass($class); $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format); @@ -182,7 +191,11 @@ $attribute = $this->nameConverter->denormalize($attribute); } - if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) { + if ((false !== $allowedAttributes && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) { + if (isset($context[self::ALLOW_EXTRA_ATTRIBUTES]) && !$context[self::ALLOW_EXTRA_ATTRIBUTES]) { + $extraAttributes[] = $attribute; + } + continue; } @@ -190,10 +203,14 @@ try { $this->setAttributeValue($object, $attribute, $value, $format, $context); } catch (InvalidArgumentException $e) { - throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); + throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e); } } + if (!empty($extraAttributes)) { + throw new ExtraAttributesException($extraAttributes); + } + return $object; } @@ -219,7 +236,7 @@ * * @return mixed * - * @throws UnexpectedValueException + * @throws NotNormalizableValueException * @throws LogicException */ private function validateAndDenormalize($currentClass, $attribute, $data, $format, array $context) @@ -253,8 +270,9 @@ throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class)); } - if ($this->serializer->supportsDenormalization($data, $class, $format)) { - return $this->serializer->denormalize($data, $class, $format, $context); + $childContext = $this->createChildContext($context, $attribute); + if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) { + return $this->serializer->denormalize($data, $class, $format, $childContext); } } @@ -273,7 +291,11 @@ } } - throw new UnexpectedValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data))); + if (!empty($context[self::DISABLE_TYPE_ENFORCEMENT])) { + return $data; + } + + throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data))); } /**