Skip to content

Commit ae2b78c

Browse files
committed
[Serializer] Add getter group example
1 parent 745f412 commit ae2b78c

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

components/serializer.rst

+19-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,18 @@ Assume you have the following plain-old-PHP object::
162162
class MyObj
163163
{
164164
public $foo;
165-
public $bar;
165+
166+
private $bar;
167+
168+
public function getBar()
169+
{
170+
return $this->bar;
171+
}
172+
173+
public function setBar($bar)
174+
{
175+
return $this->bar = $bar;
176+
}
166177
}
167178

168179
The definition of serialization can be specified using annotations, XML
@@ -207,7 +218,12 @@ Then, create your groups definition:
207218
/**
208219
* @Groups({"group3"})
209220
*/
210-
public $bar;
221+
public function getBar() // is* methods are also supported
222+
{
223+
return $this->bar;
224+
}
225+
226+
// ...
211227
}
212228
213229
.. code-block:: yaml
@@ -246,7 +262,7 @@ You are now able to serialize only attributes in the groups you want::
246262

247263
$obj = new MyObj();
248264
$obj->foo = 'foo';
249-
$obj->bar = 'bar';
265+
$obj->setBar('bar');
250266

251267
$normalizer = new ObjectNormalizer($classMetadataFactory);
252268
$serializer = new Serializer(array($normalizer));

0 commit comments

Comments
 (0)