Skip to content

Commit cab06a1

Browse files
committed
minor #8914 Add new serializer empty_data feature doc (Nek-, javiereguiluz)
This PR was merged into the 4.1 branch. Discussion ---------- Add new serializer empty_data feature doc This is the documentation related to the following feature: symfony/symfony#25493 Commits ------- 9f31bbb Fix not precise enough sentence b0d3fe8 Minor reword 5a48201 Add new serializer empty_data feature doc
2 parents 2ae4c65 + 9f31bbb commit cab06a1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

components/serializer.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,41 @@ These are the options available:
10891089
``remove_empty_tags``
10901090
If set to true, removes all empty tags in the generated XML.
10911091

1092+
Handling Constructor Arguments
1093+
------------------------------
1094+
1095+
If the constructor of a class defines arguments, as usually happens with
1096+
`Value Objects`_, the serializer won't be able to create the object if some
1097+
arguments are missing. In those cases, use the ``default_constructor_arguments``
1098+
context option::
1099+
1100+
use Symfony\Component\Serializer\Serializer;
1101+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1102+
1103+
class MyObj
1104+
{
1105+
private $foo;
1106+
private $bar;
1107+
1108+
public function __construct($foo, $bar)
1109+
{
1110+
$this->foo = $foo;
1111+
$this->bar = $bar;
1112+
}
1113+
}
1114+
1115+
$normalizer = new ObjectNormalizer($classMetadataFactory);
1116+
$serializer = new Serializer(array($normalizer));
1117+
1118+
$data = $serializer->denormalize(
1119+
array('foo' => 'Hello'),
1120+
'MyObj',
1121+
array('default_constructor_arguments' => array(
1122+
'MyObj' => array('foo' => '', 'bar' => ''),
1123+
)
1124+
));
1125+
// $data = new MyObj('Hello', '');
1126+
10921127
Recursive Denormalization and Type Safety
10931128
-----------------------------------------
10941129

@@ -1273,3 +1308,4 @@ Learn more
12731308
.. _YAML: http://yaml.org/
12741309
.. _CSV: https://tools.ietf.org/html/rfc4180
12751310
.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807
1311+
.. _`Value Objects`: https://en.wikipedia.org/wiki/Value_object

0 commit comments

Comments
 (0)