Description
Description
When using adders and removers to modify collections, PropertyAccess currently assumes that property names are English words and tries to apply English grammar transformations to singularise them (see #21569 and #5013). This is super flaky and makes this feature basically unusable in a lot of cases, as it 1. doesn't work for other languages than English (you really don't want to confuse people with English grammar if your applications domain language is something else), and 2. doesn't even work for all English words (see the ever-growing exception list).
Of course, we can't just remove the ever-growing exception list due to BC, but we could at least add another convention on top that works without natural language grammar transformations.
I propose the following one:
Calling PropertyAccessor::setValue
for property 'singularWordCollection'
should call the methods getSingularWordCollection
, addSingularWord
and removeSingularWord
.
Example
<?php
public class Veroeffentlichung {
public function __construct(
private array $kommentare,
){}
public function getKommentarCollection(): array
{
return $this->kommentare;
}
public function addKommentar(string $kommentar): void
{
$this->kommentare[] = $kommentar;
}
public function removeKommentar(string $kommentar): void
{
array_splice($this->kommentare, array_search($kommentar, $this->kommentare), 1);
}
}
$propertyAccessor = new \Symfony\Component\PropertyAccess\PropertyAccessor();
$veroeffentlichung = new Veroeffentlichung([]);
$propertyAccessor->setValue($veroeffentlichung, 'kommentarCollection', ['Nice, bro!', 'Amazing post!']);