diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 983bb9f5a0654..6cd80fdb0b8cc 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.2.0 +----- + +* `AbstractNormalizer::handleCircularReference` is now final, and receives two optional extra arguments: the format and the context + 4.1.0 ----- diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index fe8b05ef860c2..f8344cca5262b 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -190,16 +190,23 @@ protected function isCircularReference($object, &$context) * If a circular reference handler is set, it will be called. Otherwise, a * {@class CircularReferenceException} will be thrown. * - * @param object $object + * @final since Symfony 4.2 + * + * @param object $object + * @param string|null $format + * @param array $context * * @return mixed * * @throws CircularReferenceException */ - protected function handleCircularReference($object) + protected function handleCircularReference($object/*, string $format = null, array $context = array()*/) { + $format = \func_num_args() > 1 ? func_get_arg(1) : null; + $context = \func_num_args() > 2 ? func_get_arg(2) : array(); + if ($this->circularReferenceHandler) { - return \call_user_func($this->circularReferenceHandler, $object); + return \call_user_func($this->circularReferenceHandler, $object, $format, $context); } throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $this->circularReferenceLimit)); diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 7f8d341846f7d..f901712350234 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -79,13 +79,13 @@ public function normalize($object, $format = null, array $context = array()) } if ($this->isCircularReference($object, $context)) { - return $this->handleCircularReference($object); + return $this->handleCircularReference($object, $format, $context); } $data = array(); $stack = array(); $attributes = $this->getAttributes($object, $format, $context); - $class = get_class($object); + $class = \get_class($object); $attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null; foreach ($attributes as $attribute) { @@ -154,7 +154,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref */ protected function getAttributes($object, $format = null, array $context) { - $class = get_class($object); + $class = \get_class($object); $key = $class.'-'.$context['cache_key']; if (isset($this->attributesCache[$key])) { @@ -248,7 +248,7 @@ public function denormalize($data, $class, $format = null, array $context = arra $attribute = $this->nameConverter->denormalize($attribute); } - if ((false !== $allowedAttributes && !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; } @@ -345,7 +345,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute, return (float) $data; } - if (call_user_func('is_'.$builtinType, $data)) { + if (\call_user_func('is_'.$builtinType, $data)) { return $data; } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 62338af339f41..76193925435e2 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -544,6 +544,15 @@ public function testCircularReferenceHandler() $expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy'); $this->assertEquals($expected, $this->normalizer->normalize($obj)); + + $this->normalizer->setCircularReferenceHandler(function ($obj, string $format, array $context) { + $this->assertInstanceOf(CircularReferenceDummy::class, $obj); + $this->assertSame('test', $format); + $this->arrayHasKey('foo', $context); + + return \get_class($obj); + }); + $this->assertEquals($expected, $this->normalizer->normalize($obj, 'test', array('foo' => 'bar'))); } public function testDenormalizeNonExistingAttribute()