Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ private function doMap(object $source, object|string|null $target, \WeakMap $obj
}

$targetPropertyName = $mapping->target ?? $propertyName;
if (!$targetRefl->hasProperty($targetPropertyName)) {
continue;
}

$value = $this->getSourceValue($source, $mappedTarget, $value, $objectMap, $mapping);
$this->storeValue($targetPropertyName, $mapToProperties, $ctorArguments, $value);
}
Expand Down Expand Up @@ -186,7 +182,19 @@ private function doMap(object $source, object|string|null $target, \WeakMap $obj
}

foreach ($mapToProperties as $property => $value) {
$this->propertyAccessor ? $this->propertyAccessor->setValue($mappedTarget, $property, $value) : ($mappedTarget->{$property} = $value);
if ($this->propertyAccessor) {
if ($this->propertyAccessor->isWritable($mappedTarget, $property)) {
$this->propertyAccessor->setValue($mappedTarget, $property, $value);
}

continue;
}

if (!$targetRefl->hasProperty($property)) {
continue;
}

$mappedTarget->{$property} = $value;
}

return $mappedTarget;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\EmbeddedMapping;

class Address
{
public string $zipcode;
public string $city;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\EmbeddedMapping;

class User
{
public string $name;
public Address $address;

public function __construct()
{
$this->address = new Address();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\EmbeddedMapping;

use Symfony\Component\ObjectMapper\Attribute\Map;

class UserDto
{
public function __construct(
#[Map(target: 'address.zipcode')]
public string $userAddressZipcode,
#[Map(target: 'address.city')]
public string $userAddressCity,
public string $name,
) {
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\UserSource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\UserTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultValueStdClass\TargetDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\EmbeddedMapping\Address;
use Symfony\Component\ObjectMapper\Tests\Fixtures\EmbeddedMapping\User as UserEmbeddedMapping;
use Symfony\Component\ObjectMapper\Tests\Fixtures\EmbeddedMapping\UserDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\TargetUser;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\UserProfile;
Expand Down Expand Up @@ -611,4 +614,22 @@ public function testSkipLazyGhostWithClassTransform()
$this->assertSame($result->relation, $service->get());
$this->assertSame($result->relation->name, 'loaded');
}

public function testMapEmbeddedProperties()
{
$dto = new UserDto(
userAddressZipcode: '12345',
userAddressCity: 'Test City',
name: 'John Doe'
);

$mapper = new ObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessor());
$user = $mapper->map($dto, UserEmbeddedMapping::class);

$this->assertInstanceOf(UserEmbeddedMapping::class, $user);
$this->assertSame('John Doe', $user->name);
$this->assertInstanceOf(Address::class, $user->address);
$this->assertSame('12345', $user->address->zipcode);
$this->assertSame('Test City', $user->address->city);
}
}