diff --git a/components/serializer.rst b/components/serializer.rst index af35afdadc4..89a6c539cdf 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -545,10 +545,9 @@ When serializing, you can set a callback to format a specific object property:: $encoder = new JsonEncoder(); $normalizer = new GetSetMethodNormalizer(); - $callback = function ($dateTime) { - return $dateTime instanceof \DateTime - ? $dateTime->format(\DateTime::ISO8601) - : ''; + // all callback parameters are optional (you can omit the ones you don't use) + $callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = array()) { + return $dateTime instanceof \DateTime ? $dateTime->format(\DateTime::ISO8601) : ''; }; $normalizer->setCallbacks(array('createdAt' => $callback)); @@ -563,6 +562,10 @@ When serializing, you can set a callback to format a specific object property:: $serializer->serialize($person, 'json'); // Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"} +.. versionadded:: 4.2 + The ``$outerObject``, ``$attributeName``, ``$format`` and ``$context`` + parameters of the callback were introduced in Symfony 4.2. + .. _component-serializer-normalizers: Normalizers @@ -828,7 +831,8 @@ having unique identifiers:: $encoder = new JsonEncoder(); $normalizer = new ObjectNormalizer(); - $normalizer->setCircularReferenceHandler(function ($object) { + // all callback parameters are optional (you can omit the ones you don't use) + $normalizer->setCircularReferenceHandler(function ($object, string $format = null, array $context = array()) { return $object->getName(); }); @@ -836,6 +840,10 @@ having unique identifiers:: var_dump($serializer->serialize($org, 'json')); // {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]} +.. versionadded:: 4.2 + The ``$format`` and ``$context`` parameters of ``setCircularReferenceHandler()`` + were introduced in Symfony 4.2. + Handling Serialization Depth ---------------------------- @@ -964,8 +972,9 @@ having unique identifiers:: $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); $normalizer = new ObjectNormalizer($classMetadataFactory); - $normalizer->setMaxDepthHandler(function ($foo) { - return '/foos/'.$foo->id; + // all callback parameters are optional (you can omit the ones you don't use) + $normalizer->setMaxDepthHandler(function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = array()) { + return '/foos/'.$innerObject->id; }); $serializer = new Serializer(array($normalizer)); @@ -984,6 +993,10 @@ having unique identifiers:: .. versionadded:: 4.1 The ``setMaxDepthHandler()`` method was introduced in Symfony 4.1. +.. versionadded:: 4.2 + The ``$outerObject``, ``$attributeName``, ``$format`` and ``$context`` + parameters of ``setMaxDepthHandler()`` were introduced in Symfony 4.2. + Handling Arrays ---------------