Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9bcde6

Browse files
committed
[Serializer] Max depth handler support
1 parent9abccc8 commitd9bcde6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎components/serializer.rst‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,54 @@ because it is deeper than the configured maximum depth of 2::
813813
);
814814
*/
815815

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+
816864
Handling Arrays
817865
---------------
818866

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp