Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping arrays to arrays differs if using direct assignment vs adder/remover #227

Open
allflame opened this issue Feb 24, 2025 · 0 comments

Comments

@allflame
Copy link

There is incosistency in behavior between

class From
{
    public array $values;
}

class To
{
    public array $values;
}

$from = new From();
$to = new To();
$from->values = [1, 2, 3];
$to->values = [4, 5, 6];
$result = AutoMapper::create()->map($from, $to);
// $result->values is [1, 2, 3]

and approach with private/protected property and adder/remover

class From
{
    public array $values;
}

class To
{
    private array $values;

    public function addValue(mixed $value): void
    {
        $this->values[] = $value;
    }

    public function removeValue(mixed $value): void
    {
        foreach ($this->values as $key => $existing) {
            if ($value === $value) {
                unset($this->values[$key]);
            }
        }
    }

    public function getValues(): array
    {
        return $this->values;
    }
}

$from = new From();
$to = new To();
$from->values = [1, 2, 3];
$to->values = [4, 5, 6];
$result = AutoMapper::create()->map($from, $to);
// $result->values is [4, 5, 6, 1, 2, 3]

I believe this is also the cause for the issue outlined here

It seems like removerInfo is discarded and never used in the MappingExtractor leading to duplications.
Real use-case problem is mapping relations x-to-Many relations from DTOs to Doctrine collections, whereby whatever the expected behavior might be (and problems associated with it 169, 198, ), it is definitely not a duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant