8000 [Serializer] Allow to access to the context and various other infos in callbacks and max depth handler by dunglas · Pull Request #10318 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Allow to access to the context and various other infos in callbacks and max depth handler #10318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Serializer] Allow to access to the context and various other infos i…
…n callbacks and max depth handler
  • Loading branch information
dunglas committed Sep 10, 2018
commit a4dad43a2742658a4ddb2c738bb1cd0842205cc6
21 changes: 15 additions & 6 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
: '';
$callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = null) {
// Every parameters can be omitted if not used
return $dateTime instanceof \DateTime ? $dateTime->format(\DateTime::ISO8601) : '';
};

$normalizer->setCallbacks(array('createdAt' => $callback));
Expand All @@ -563,6 +562,11 @@ 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
Expand Down Expand Up @@ -964,8 +968,9 @@ having unique identifiers::

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$normalizer->setMaxDepthHandler(function ($foo) {
return '/foos/'.$foo->id;
$normalizer->setMaxDepthHandler(function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = null) {
// Every parameters can be omitted if not used
return '/foos/'.$innerObject->id;
});

$serializer = new Serializer(array($normalizer));
Expand All @@ -984,6 +989,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
---------------

Expand Down
0