Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit62f43d9

Browse files
committed
Non-standard adder/remover methods
1 parentb94ccd4 commit62f43d9

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎components/property_access.rst

Lines changed: 45 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp