Skip to content

Commit 7370af6

Browse files
committed
feature #13137 [PropertyAccess] Non-standard adder/remover methods (Korbeil)
This PR was merged into the 5.1 branch. Discussion ---------- [PropertyAccess] Non-standard adder/remover methods Will add documentation about non-standard adder/remover methods for PropertyAccessor. Related to #13023 and symfony/symfony#9336 This is not a complete documentation for related issues, but this will introduce one of the possible new use. Commits ------- bf6cca9 Non-standard adder/remover methods
2 parents b708b67 + bf6cca9 commit 7370af6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

components/property_access.rst

+39
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,45 @@ and ``removeChild()`` methods to access to the ``children`` property.
394394

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

397+
Using non-standard adder/remover methods
398+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399+
400+
Sometimes, adder and remover methods don't use the standard ``add`` or ``remove`` prefix, like in this example::
401+
402+
// ...
403+
class PeopleList
404+
{
405+
// ...
406+
407+
public function joinPeople(string $people): void
408+
{
409+
$this->peoples[] = $people;
410+
}
411+
412+
public function leavePeople(string $people): void
413+
{
414+
foreach ($this->peoples as $id => $item) {
415+
if ($people === $item) {
416+
unset($this->peoples[$id]);
417+
break;
418+
}
419+
}
420+
}
421+
}
422+
423+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
424+
use Symfony\Component\PropertyAccess\PropertyAccessor;
425+
426+
$list = new PeopleList();
427+
$reflectionExtractor = new ReflectionExtractor(null, null, ['join', 'leave']);
428+
$propertyAccessor = new PropertyAccessor(false, false, null, true, $reflectionExtractor, $reflectionExtractor);
429+
$propertyAccessor->setValue($person, 'peoples', ['kevin', 'wouter']);
430+
431+
var_dump($person->getPeoples()); // ['kevin', 'wouter']
432+
433+
Instead of calling ``add<SingularOfThePropertyName>()`` and ``remove<SingularOfThePropertyName>()``, the PropertyAccess
434+
component will call ``join<SingularOfThePropertyName>()`` and ``leave<SingularOfThePropertyName>()`` methods.
435+
397436
Checking Property Paths
398437
-----------------------
399438

0 commit comments

Comments
 (0)