Skip to content

Commit 62f43d9

Browse files
committed
Non-standard adder/remover methods
1 parent b94ccd4 commit 62f43d9

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

components/property_access.rst

+45
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,51 @@ 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+
Like we just said, ``PropertyAccessor`` class allows to update the content of arrays stored in properties through
401+
*adder* and *remover* methods. But sometimes theses methods doesn't use the standard ``add`` and ``remove`` prefix::
402+
403+
// ...
404+
class PeopleList
405+
{
406+
/**
407+
* @var string[]
408+
*/
409+
private $peoples = [];
410+
411+
public function getPeoples(): array
412+
{
413+
return $this->peoples;
414+
}
415+
416+
public function joinPeople(string $people): void
417+
{
418+
$this->peoples[] = $people;
419+
}
420+
421+
public function leavePeople(string $people): void
422+
{
423+
foreach ($this->peoples as $id => $item) {
424+
if ($people === $item) {
425+
unset($this->peoples[$id]);
426+
break;
427+
}
428+
}
429+
}
430+
}
431+
432+
$list = new PeopleList();
433+
$reflectionExtractor = new ReflectionExtractor(null, null, ['join', 'leave']);
434+
$propertyAccessor = new PropertyAccessor(false, false, null, true, $reflectionExtractor, $reflectionExtractor);
435+
$propertyAccessor->setValue($person, 'peoples', ['kevin', 'wouter']);
436+
437+
var_dump($person->getPeoples()); // ['kevin', 'wouter']
438+
439+
Instead of call ``add<SingularOfThePropertyName>()`` and ``remove<SingularOfThePropertyName>()``, the PropertyAccess
440+
component will call ``join<SingularOfThePropertyName>()`` and ``leave<SingularOfThePropertyName>()`` methods.
441+
397442
Checking Property Paths
398443
-----------------------
399444

0 commit comments

Comments
 (0)