@@ -655,7 +655,7 @@ When serializing, you can set a callback to format a specific object property::
655
655
$encoder = new JsonEncoder();
656
656
657
657
// all callback parameters are optional (you can omit the ones you don't use)
658
- $callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
658
+ $dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
659
659
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
660
660
};
661
661
@@ -664,7 +664,7 @@ When serializing, you can set a callback to format a specific object property::
664
664
'createdAt' => $dateCallback,
665
665
],
666
666
];
667
-
667
+
668
668
$normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
669
669
670
670
$serializer = new Serializer([$normalizer], [$encoder]);
@@ -677,6 +677,26 @@ When serializing, you can set a callback to format a specific object property::
677
677
$serializer->serialize($person, 'json');
678
678
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
679
679
680
+ Also, you can pass an \A rrayObject or its subclass to callbacks context field to get more flexibility::
681
+
682
+ $defaultContext = [
683
+ AbstractNormalizer::CALLBACKS => new class() extends \ArrayObject {
684
+ public function offsetExists($index)
685
+ {
686
+ return true;
687
+ }
688
+
689
+ public function offsetGet($index)
690
+ {
691
+ return function () use ($index) {
692
+ return $index;
693
+ };
694
+ }
695
+ }
696
+ ];
697
+
698
+ $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
699
+
680
700
.. deprecated :: 4.2
681
701
682
702
The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCallbacks ` is deprecated since
0 commit comments