@@ -314,6 +314,51 @@ see `Enable other Features`_.
314
314
315
315
var_dump($person->getWouter()); // array(...)
316
316
317
+ Writing to Array Properties
318
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
319
+
320
+ The ``PropertyAccessor `` class allows to update the content of arrays stored in
321
+ properties through *adder * and *remover * methods.
322
+
323
+ .. code-block :: php
324
+
325
+ // ...
326
+ class Person
327
+ {
328
+ /**
329
+ * @var string[]
330
+ */
331
+ private $children = array();
332
+
333
+ public function getChildren(): array
334
+ {
335
+ return $this->children;
336
+ }
337
+
338
+ public function addChild(string $name): void
339
+ {
340
+ $this->children[$name] = $name;
341
+ }
342
+
343
+ public function removeChild(string $name): void
344
+ {
345
+ unset($this->children[$name]);
346
+ }
347
+ }
348
+
349
+ $person = new Person();
350
+ $accessor->setValue($person, 'children', array('kevin', 'wouter'));
351
+
352
+ var_dump($person->getChildren()); // array('kevin', 'wouter')
353
+
354
+ The PropertyAccess component checks for methods called ``add<SingularOfThePropertyName>() ``
355
+ and ``remove<SingularOfThePropertyName>() ``. Both methods must be defined.
356
+ For instance, in the previous example, the component looks for the ``addChild() ``
357
+ and ``removeChild() `` methods to access to the ``children `` property.
358
+ `The Inflector component `_ is used to find the singular of a property name.
359
+
360
+ If available, *adder * and *remover * methods have priority over a *setter * method.
361
+
317
362
Checking Property Paths
318
363
-----------------------
319
364
@@ -407,3 +452,4 @@ Or you can pass parameters directly to the constructor (not the recommended way)
407
452
408
453
409
454
.. _Packagist : https://packagist.org/packages/symfony/property-access
455
+ .. _The Inflector component : https://github.com/symfony/inflector
0 commit comments