Skip to content

[PropertyAccess] Non-standard adder/remover methods #13137

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
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
Diff view
39 changes: 39 additions & 0 deletions components/property_access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,45 @@ and ``removeChild()`` methods to access to the ``children`` property.

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

Using non-standard adder/remover methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes, adder and remover methods don't use the standard ``add`` or ``remove`` prefix, like in this example::

// ...
class PeopleList
{
// ...

public function joinPeople(string $people): void
{
$this->peoples[] = $people;
}

public function leavePeople(string $people): void
{
foreach ($this->peoples as $id => $item) {
if ($people === $item) {
unset($this->peoples[$id]);
break;
}
}
}
}

use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyAccess\PropertyAccessor;

$list = new PeopleList();
$reflectionExtractor = new ReflectionExtractor(null, null, ['join', 'leave']);
$propertyAccessor = new PropertyAccessor(false, false, null, true, $reflectionExtractor, $reflectionExtractor);
$propertyAccessor->setValue($person, 'peoples', ['kevin', 'wouter']);

var_dump($person->getPeoples()); // ['kevin', 'wouter']

Instead of calling ``add<SingularOfThePropertyName>()`` and ``remove<SingularOfThePropertyName>()``, the PropertyAccess
component will call ``join<SingularOfThePropertyName>()`` and ``leave<SingularOfThePropertyName>()`` methods.

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

Expand Down