Skip to content

Commit fc996f7

Browse files
committed
minor #11394 edit normalizer deprecated method on 4.2 (Raulnet)
This PR was merged into the 4.2 branch. Discussion ---------- edit normalizer deprecated method on 4.2 i have added documentation for deprecated method in Normalizer `Symfony\Component\Serializer\Normalizer\AbstractNormalizer::setCallbacks` `Symfony\Component\Serializer\Normalizer\AbstractNormalizer::setCircularReferenceLimit` `Symfony\Component\Serializer\Normalizer\AbstractNormalizer::setMaxDepthHandler` And edit the code for the new params declaration; And move the text `..deprecated 4.2 ...` below the demo code for more consistency Commits ------- f02e6fd edit normalizer deprecated method on 4.2
2 parents 31175cf + f02e6fd commit fc996f7

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

components/serializer.rst

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ When serializing, you can set a callback to format a specific object property::
639639
$encoder = new JsonEncoder();
640640

641641
// all callback parameters are optional (you can omit the ones you don't use)
642-
$callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
642+
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
643643
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
644644
};
645645

@@ -661,6 +661,11 @@ When serializing, you can set a callback to format a specific object property::
661661
$serializer->serialize($person, 'json');
662662
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
663663

664+
.. deprecated:: 4.2
665+
666+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCallbacks` is deprecated since
667+
Symfony 4.2, use the "callbacks" key of the context instead.
668+
664669
.. _component-serializer-normalizers:
665670

666671
Normalizers
@@ -931,15 +936,9 @@ when such a case is encountered::
931936

932937
echo $serializer->serialize($organization, 'json'); // Throws a CircularReferenceException
933938

934-
The ``setCircularReferenceLimit()`` method of this normalizer sets the number
935-
of times it will serialize the same object before considering it a circular
936-
reference. Its default value is ``1``.
937-
938-
.. deprecated:: 4.2
939-
940-
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler`
941-
method is deprecated since Symfony 4.2. Use the ``circular_reference_handler``
942-
key of the context instead.
939+
The key ``circular_reference_limit`` in the default context sets the number of
940+
times it will serialize the same object before considering it a circular
941+
reference. The default value is ``1``.
943942

944943
Instead of throwing an exception, circular references can also be handled
945944
by custom callables. This is especially useful when serializing entities
@@ -957,6 +956,12 @@ having unique identifiers::
957956
var_dump($serializer->serialize($org, 'json'));
958957
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
959958

959+
.. deprecated:: 4.2
960+
961+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler`
962+
method is deprecated since Symfony 4.2. Use the ``circular_reference_handler``
963+
key of the context instead.
964+
960965
Handling Serialization Depth
961966
----------------------------
962967

@@ -1084,11 +1089,16 @@ having unique identifiers::
10841089
$level2->child = $level3;
10851090

10861091
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1087-
$normalizer = new ObjectNormalizer($classMetadataFactory);
1092+
10881093
// all callback parameters are optional (you can omit the ones you don't use)
1089-
$normalizer->setMaxDepthHandler(function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
1094+
$maxDepthHandler = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
10901095
return '/foos/'.$innerObject->id;
1091-
});
1096+
};
1097+
1098+
$defaultContext = [
1099+
AbstractObjectNormalizer::MAX_DEPTH_HANDLER => $maxDepthHandler,
1100+
];
1101+
$normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, $defaultContext);
10921102

10931103
$serializer = new Serializer([$normalizer]);
10941104

@@ -1103,6 +1113,12 @@ having unique identifiers::
11031113
];
11041114
*/
11051115

1116+
.. deprecated:: 4.2
1117+
1118+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setMaxDepthHandler`
1119+
method is deprecated since Symfony 4.2. Use the ``max_depth_handler``
1120+
key of the context instead.
1121+
11061122
Handling Arrays
11071123
---------------
11081124

0 commit comments

Comments
 (0)