Open
Description
Hi all,
This is my use case :
class Source
{
#[MapTo(target: 'Target', groups: ['write'])]
public string $name;
public string $street;
public string $zip;
public string $city;
public string $country;
}
class TargetAddress {
public string $street;
public string $zip;
public string $city;
public string $country;
}
class Target
{
public string $name;
public ?TargetAddress $address = null;
}
$target = $automapper->map($source, new Target(), ['groups' => ['write']]);
I have to map the address fields from Source into an object in Target. There is a limitation : the target class can't be modified beause it comes from an external lib.
First I thought to do that :
class Source
{
#[MapTo(target: 'Target', groups: ['write'])]
public string $name;
public string $street;
public string $zip;
public string $city;
public string $country;
#[MapTo(target: 'Target', property: 'address', groups: ['write'])]
public function getAddress(): array
{
return [
'street' => $this->street,
'zip' => $this->zip,
'city' => $this->city,
'country' => $this->country,
];
}
}
But I doesn't work, It can convert the array to a new TargetAddress instance.
Then I tried :
class Source
{
#[MapTo(target: 'Target', groups: ['write'])]
public string $name;
public string $street;
public string $zip;
public string $city;
public string $country;
#[MapTo(target: 'Target', property: 'address', groups: ['write'])]
public function getAddress(): \stdClass
{
return (object)[
'street' => $this->street,
'zip' => $this->zip,
'city' => $this->city,
'country' => $this->country,
];
}
}
Here the TargetAddress is created, but because of the group, the properties are not mapped.
Is there a way to do that simply ?
Thanks in advance