From 5a48201d6a2082a2226c19aa818e5d6a690955b4 Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Wed, 20 Dec 2017 21:03:34 +0100 Subject: [PATCH 1/3] Add new serializer empty_data feature doc --- components/serializer.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index 1c24ab431ed..0e224f3a4bc 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1089,6 +1089,36 @@ These are the options available: ``remove_empty_tags`` If set to true, removes all empty tags in the generated XML. +Handling Value Objects +---------------------- + +Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one +of theses parameters the serializer will throw an exception because it can't create the object. + +To support Value Objects you will need to define the context option ``default_constructor_arguments``:: + + use Symfony\Component\Serializer\Serializer; + use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; + + class MyObj { + private $foo; + private $bar; + + public function __construct($foo, $bar) + { + $this->foo = $foo; + $this->bar = $bar; + } + } + + $normalizer = new ObjectNormalizer($classMetadataFactory); + $serializer = new Serializer(array($normalizer)); + + $data = $serializer->denormalize(['foo' => 'Hello'], 'MyObj', array('default_constructor_arguments' => array( + 'MyObj' => array('foo' => '', 'bar' => ''), + ))); + // $data = new MyObj('Hello', ''); + Recursive Denormalization and Type Safety ----------------------------------------- From b0d3fe8ae12c96271935c708a10607e68e000718 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 20 Feb 2018 16:52:01 +0100 Subject: [PATCH 2/3] Minor reword --- components/serializer.rst | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 0e224f3a4bc..55b42c8acb7 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1089,18 +1089,18 @@ These are the options available: ``remove_empty_tags`` If set to true, removes all empty tags in the generated XML. -Handling Value Objects ----------------------- - -Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one -of theses parameters the serializer will throw an exception because it can't create the object. +Handling Constructor Arguments +------------------------------ -To support Value Objects you will need to define the context option ``default_constructor_arguments``:: +If the constructor of a class defines arguments, as usually happens with +`Value Objects`_, the serializer won't be able to create the object. In those +cases, use the ``default_constructor_arguments`` context option:: use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; - class MyObj { + class MyObj + { private $foo; private $bar; @@ -1114,9 +1114,13 @@ To support Value Objects you will need to define the context option ``default_co $normalizer = new ObjectNormalizer($classMetadataFactory); $serializer = new Serializer(array($normalizer)); - $data = $serializer->denormalize(['foo' => 'Hello'], 'MyObj', array('default_constructor_arguments' => array( - 'MyObj' => array('foo' => '', 'bar' => ''), - ))); + $data = $serializer->denormalize( + array('foo' => 'Hello'), + 'MyObj', + array('default_constructor_arguments' => array( + 'MyObj' => array('foo' => '', 'bar' => ''), + ) + )); // $data = new MyObj('Hello', ''); Recursive Denormalization and Type Safety @@ -1302,3 +1306,4 @@ Learn more .. _YAML: http://yaml.org/ .. _CSV: https://tools.ietf.org/html/rfc4180 .. _`RFC 7807`: https://tools.ietf.org/html/rfc7807 +.. _`Value Objects`: https://en.wikipedia.org/wiki/Value_object From 9f31bbb613c7f8257e2f4a94689f84dee6407a1d Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Mon, 4 Jun 2018 19:23:08 +0200 Subject: [PATCH 3/3] Fix not precise enough sentence --- components/serializer.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 55b42c8acb7..17a46c8a5ed 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1093,8 +1093,9 @@ Handling Constructor Arguments ------------------------------ If the constructor of a class defines arguments, as usually happens with -`Value Objects`_, the serializer won't be able to create the object. In those -cases, use the ``default_constructor_arguments`` context option:: +`Value Objects`_, the serializer won't be able to create the object if some +arguments are missing. In those cases, use the ``default_constructor_arguments`` +context option:: use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;