Skip to content

Commit 99f829e

Browse files
committed
[Serializer] Allow to access to the format and context in circular ref handler
1 parent 26989d4 commit 99f829e

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* `AbstractNormalizer::handleCircularReference` is now final, and receives two optional extra arguments: the format and the context
8+
49
4.1.0
510
-----
611

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,23 @@ protected function isCircularReference($object, &$context)
190190
* If a circular reference handler is set, it will be called. Otherwise, a
191191
* {@class CircularReferenceException} will be thrown.
192192
*
193-
* @param object $object
193+
* @final since Symfony 4.2
194+
*
195+
* @param object $object
196+
* @param string|null $format
197+
* @param array $context
194198
*
195199
* @return mixed
196200
*
197201
* @throws CircularReferenceException
198202
*/
199-
protected function handleCircularReference($object)
203+
protected function handleCircularReference($object/*, string $format = null, array $context = array()*/)
200204
{
205+
$format = \func_num_args() > 1 ? func_get_arg(1) : null;
206+
$context = \func_num_args() > 2 ? func_get_arg(2) : array();
207+
201208
if ($this->circularReferenceHandler) {
202-
return \call_user_func($this->circularReferenceHandler, $object);
209+
return \call_user_func($this->circularReferenceHandler, $object, $format, $context);
203210
}
204211

205212
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));

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ public function normalize($object, $format = null, array $context = array())
7979
}
8080

8181
if ($this->isCircularReference($object, $context)) {
82-
return $this->handleCircularReference($object);
82+
return $this->handleCircularReference($object, $format, $context);
8383
}
8484

8585
$data = array();
8686
$stack = array();
8787
$attributes = $this->getAttributes($object, $format, $context);
88-
$class = get_class($object);
88+
$class = \get_class($object);
8989
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
9090

9191
foreach ($attributes as $attribute) {
@@ -154,7 +154,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
154154
*/
155155
protected function getAttributes($object, $format = null, array $context)
156156
{
157-
$class = get_class($object);
157+
$class = \get_class($object);
158158
$key = $class.'-'.$context['cache_key'];
159159

160160
if (isset($this->attributesCache[$key])) {
@@ -248,7 +248,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
248248
$attribute = $this->nameConverter->denormalize($attribute);
249249
}
250250

251-
if ((false !== $allowedAttributes && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
251+
if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
252252
if (isset($context[self::ALLOW_EXTRA_ATTRIBUTES]) && !$context[self::ALLOW_EXTRA_ATTRIBUTES]) {
253253
$extraAttributes[] = $attribute;
254254
}
@@ -345,7 +345,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
345345
return (float) $data;
346346
}
347347

348-
if (call_user_func('is_'.$builtinType, $data)) {
348+
if (\call_user_func('is_'.$builtinType, $data)) {
349349
return $data;
350350
}
351351
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,15 @@ public function testCircularReferenceHandler()
544544

545545
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
546546
$this->assertEquals($expected, $this->normalizer->normalize($obj));
547+
548+
$this->normalizer->setCircularReferenceHandler(function ($obj, string $format, array $context) {
549+
$this->assertInstanceOf(CircularReferenceDummy::class, $obj);
550+
$this->assertSame('test', $format);
551+
$this->arrayHasKey('foo', $context);
552+
553+
return \get_class($obj);
554+
});
555+
$this->assertEquals($expected, $this->normalizer->normalize($obj, 'test', array('foo' => 'bar')));
547556
}
548557

549558
public function testDenormalizeNonExistingAttribute()

0 commit comments

Comments
 (0)