Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a48201

Browse files
committed
Add new serializer empty_data feature doc
1 parent 0401973 commit 5a48201

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

components/serializer.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,36 @@ 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 Value Objects
1093+
----------------------
1094+
1095+
Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one
1096+
of theses parameters the serializer will throw an exception because it can't create the object.
1097+
1098+
To support Value Objects you will need to define the context option ``default_constructor_arguments``::
1099+
1100+
use Symfony\Component\Serializer\Serializer;
1101+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1102+
1103+
class MyObj {
1104+
private $foo;
1105+
private $bar;
1106+
1107+
public function __construct($foo, $bar)
1108+
{
1109+
$this->foo = $foo;
1110+
$this->bar = $bar;
1111+
}
1112+
}
1113+
1114+
$normalizer = new ObjectNormalizer($classMetadataFactory);
1115+
$serializer = new Serializer(array($normalizer));
1116+
1117+
$data = $serializer->denormalize(['foo' => 'Hello'], 'MyObj', array('default_constructor_arguments' => array(
1118+
'MyObj' => array('foo' => '', 'bar' => ''),
1119+
)));
1120+
// $data = new MyObj('Hello', '');
1121+
10921122
Recursive Denormalization and Type Safety
10931123
-----------------------------------------
10941124

0 commit comments

Comments
 (0)