8000 minor #10318 [Serializer] Allow to access to the context and various … · symfony/symfony-docs@97c4e68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97c4e68

Browse files
committed
minor #10318 [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler (dunglas, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler symfony/symfony#27020 symfony/symfony#27017 Commits ------- 672b374 Minor reword 7c3e620 Fix, and document setCircularReferenceHandler's new parameters a4dad43 [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler
2 parents 5bc7ac0 + 672b374 commit 97c4e68

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

components/serializer.rst

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,9 @@ When serializing, you can set a callback to format a specific object property::
545545
$encoder = new JsonEncoder();
546546
$normalizer = new GetSetMethodNormalizer();
547547

548-
$callback = function ($dateTime) {
549-
return $dateTime instanceof \DateTime
550-
? $dateTime->format(\DateTime::ISO8601)
551-
: '';
548+
// all callback parameters are optional (you can omit the ones you don't use)
549+
$callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = array()) {
550+
return $dateTime instanceof \DateTime ? $dateTime->format(\DateTime::ISO8601) : '';
552551
};
553552

554553
$normalizer->setCallbacks(array('createdAt' => $callback));
@@ -563,6 +562,10 @@ When serializing, you can set a callback to format a specific object property::
563562
$serializer->serialize($person, 'json');
564563
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
565564

565+
.. versionadded:: 4.2
566+
The ``$outerObject``, ``$attributeName``, ``$format`` and ``$context``
567+
parameters of the callback were introduced in Symfony 4.2.
568+
566569
.. _component-serializer-normalizers:
567570

568571
Normalizers
@@ -828,14 +831,19 @@ having unique identifiers::
828831
$encoder = new JsonEncoder();
829832
$normalizer = new ObjectNormalizer();
830833

831-
$normalizer->setCircularReferenceHandler(function ($object) {
834+
// all callback parameters are optional (you can omit the ones you don't use)
835+
$normalizer->setCircularReferenceHandler(function ($object, string $format = null, array $context = array()) {
832836
return $object->getName();
833837
});
834838

835839
$serializer = new Serializer(array($normalizer), array($encoder));
836840
var_dump($serializer->serialize($org, 'json'));
837841
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
838842

843+
.. versionadded:: 4.2
844+
The ``$format`` and ``$context`` parameters of ``setCircularReferenceHandler()``
845+
were introduced in Symfony 4.2.
846+
839847
Handling Serialization Depth
840848
----------------------------
841849

@@ -964,8 +972,9 @@ having unique identifiers::
964972

965973
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
966974
$normalizer = new ObjectNormalizer($classMetadataFactory);
967-
$normalizer->setMaxDepthHandler(function ($foo) {
968-
return '/foos/'.$foo->id;
975+
// all callback parameters are optional (you can omit the ones you don't use)
976+
$normalizer->setMaxDepthHandler(function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = array()) {
977+
return '/foos/'.$innerObject->id;
969978
});
970979

971980
$serializer = new Serializer(array($normalizer));
@@ -984,6 +993,10 @@ having unique identifiers::
984993
.. versionadded:: 4.1
985994
The ``setMaxDepthHandler()`` method was introduced in Symfony 4.1.
986995

996+
.. versionadded:: 4.2
997+
The ``$outerObject``, ``$attributeName``, ``$format`` and ``$context``
998+
parameters of ``setMaxDepthHandler()`` were introduced in Symfony 4.2.
999+
9871000
Handling Arrays
9881001
---------------
9891002

0 commit comments

Comments
 (0)
0