Skip to content

[PropertyAccess] Add a predictable convention for Adders and Removers  #52809

Open
@janopae

Description

@janopae

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!']);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy