File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -813,6 +813,54 @@ because it is deeper than the configured maximum depth of 2::
813
813
);
814
814
*/
815
815
816
+ Instead of throwing an exception, a custom callable can be called when the maximum depth is reached. This is especially
817
+ useful when serializing entities having unique identifiers::
818
+
819
+ use Doctrine\Common\Annotations\AnnotationReader;
820
+ use Symfony\Component\Serializer\Serializer;
821
+ use Symfony\Component\Serializer\Annotation\MaxDepth;
822
+ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
823
+ use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
824
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
825
+
826
+ class Foo
827
+ {
828
+ public $id;
829
+ /**
830
+ * @MaxDepth(1)
831
+ */
832
+ public $child;
833
+ }
834
+
835
+ $level1 = new Foo();
836
+ $level1->id = 1;
837
+
838
+ $level2 = new Foo();
839
+ $level2->id = 2;
840
+ $level1->child = $level2;
841
+
842
+ $level3 = new Foo();
843
+ $level3->id = 3;
844
+ $level2->child = $level3;
845
+
846
+ $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
847
+ $normalizer = new ObjectNormalizer($classMetadataFactory);
848
+ $normalizer->setMaxDepthHandler(function ($foo) {
849
+ return '/foos/'.$foo->id;
850
+ });
851
+
852
+ $serializer = new Serializer(array($normalizer));
853
+
854
+ $result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
855
+ /*
856
+ $result = array(
857
+ 'id' => 1,
858
+ 'child' => array(
859
+ 'id' => 2,
860
+ 'child' => '/foos/3',
861
+ );
862
+ */
863
+
816
864
Handling Arrays
817
865
---------------
818
866
You can’t perform that action at this time.
0 commit comments