@@ -913,18 +913,18 @@ These are the options available:
913
913
``remove_empty_tags ``
914
914
If set to true, removes all empty tags in the generated XML.
915
915
916
- Handling Value Objects
917
- ----------------------
918
-
919
- Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one
920
- of theses parameters the serializer will throw an exception because it can't create the object.
916
+ Handling Constructor Arguments
917
+ ------------------------------
921
918
922
- To support Value Objects you will need to define the context option ``default_constructor_arguments ``::
919
+ If the constructor of a class defines arguments, as usually happens with
920
+ `Value Objects `_, the serializer won't be able to create the object. In those
921
+ cases, use the ``default_constructor_arguments `` context option::
923
922
924
923
use Symfony\Component\Serializer\Serializer;
925
924
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
926
925
927
- class MyObj {
926
+ class MyObj
927
+ {
928
928
private $foo;
929
929
private $bar;
930
930
@@ -938,9 +938,13 @@ To support Value Objects you will need to define the context option ``default_co
938
938
$normalizer = new ObjectNormalizer($classMetadataFactory);
939
939
$serializer = new Serializer(array($normalizer));
940
940
941
- $data = $serializer->denormalize(['foo' => 'Hello'], 'MyObj', array('default_constructor_arguments' => array(
942
- 'MyObj' => array('foo' => '', 'bar' => ''),
943
- )));
941
+ $data = $serializer->denormalize(
942
+ array('foo' => 'Hello'),
943
+ 'MyObj',
944
+ array('default_constructor_arguments' => array(
945
+ 'MyObj' => array('foo' => '', 'bar' => ''),
946
+ )
947
+ ));
944
948
// $data = new MyObj('Hello', '');
945
949
946
950
Recursive Denormalization and Type Safety
@@ -1116,3 +1120,4 @@ Learn more
1116
1120
.. _XML : https://www.w3.org/XML/
1117
1121
.. _YAML : http://yaml.org/
1118
1122
.. _CSV : https://tools.ietf.org/html/rfc4180
1123
+ .. _`Value Objects` : https://en.wikipedia.org/wiki/Value_object
0 commit comments