8000 [PropertyAccess] Document adder and remover support by dunglas · Pull Request #8349 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Document adder and remover support #8349

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
46 changes: 46 additions & 0 deletions components/property_access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,51 @@ see `Enable other Features`_.

var_dump($person->getWouter()); // array(...)

Writing to Array Properties
~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ``PropertyAccessor`` class allows to update the content of arrays stored in
properties through *adder* and *remover* methods.

.. code-block:: php

// ...
class Person
{
/**
* @var string[]
*/
private $children = array();

public function getChildren(): array
{
return $this->children;
}

public function addChild(string $name): void
{
$this->children[$name] = $name;
}

public function removeChild(string $name): void
{
unset($this->children[$name]);
}
}

$person = new Person();
$accessor->setValue($person, 'children', array('kevin', 'wouter'));

var_dump($person->getChildren()); // array('kevin', 'wouter')

The PropertyAccess component checks for methods called ``add<SingularOfThePropertyName>()``
and ``remove<SingularOfThePropertyName>()``. Both methods must be defined.
For instance, in the previous example, the component looks for ``addChild()`` and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] looks for the [...]

``removeChild()`` methods to access to the ``children`` property.
`The Inflector component`_ is used to find the singular of a property name.

If available, *adder* and *remover* methods have priority over a *setter* method.

Checking Property Paths
-----------------------

Expand Down Expand Up @@ -414,3 +459,4 @@ Or you can pass parameters directly to the constructor (not the recommended way)


.. _Packagist: https://packagist.org/packages/symfony/property-access
.. _The Inflector component: https://github.com/symfony/inflector
0