Permalink
Browse files

feature #27020 [Serializer] Allow to access to the format and context…

… in circular ref handler (dunglas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Serializer] Allow to access to the format and context in circular ref handler

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | todo

Similar to #27017 but for circular reference handlers.

ping @meyerbaptiste

Commits
-------

99f829e [Serializer] Allow to access to the format and context in circular ref handler
  • Loading branch information...
nicolas-grekas committed Jul 7, 2018
2 parents 6d3f63d + 99f829e commit ac1189a61e74b83be17787473d1aa9ceabd050c9
@@ -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
-----
@@ -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));
@@ -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;
}
}
@@ -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()

0 comments on commit ac1189a

Please sign in to comment.