Skip to content
Open
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
1 change: 1 addition & 0 deletions src/Symfony/Component/ObjectMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* The component is not marked as `@experimental` anymore
* Add `ObjectMapperAwareInterface` to set the owning object mapper instance
* Add a `MapCollection` transform that calls the Mapper over iterable properties
* Add support for mapping nested objects

7.3
---
Expand Down
59 changes: 59 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,65 @@ private function doMap(object $source, object|string|null $target, \WeakMap $obj
$value = $this->getSourceValue($source, $mappedTarget, $this->getRawValue($source, $propertyName), $objectMap);
$this->storeValue($propertyName, $mapToProperties, $ctorArguments, $value);
}

if (!$mappings && !$targetRefl->hasProperty($propertyName)) {
$sourceProperty = $refl->getProperty($propertyName);
if ($refl->isInstance($source) && !$sourceProperty->isInitialized($source)) {
continue;
}

try {
$value = $this->getRawValue($source, $propertyName);
} catch (NoSuchPropertyException) {
continue;
}

if (!\is_object($value)) {
continue;
}

try {
$nestedRefl = new \ReflectionClass($value);
} catch (\ReflectionException) {
continue;
}

foreach ($nestedRefl->getProperties() as $nestedProperty) {
if ($nestedProperty->isStatic()) {
continue;
}

$nestedPropertyName = $nestedProperty->getName();
$nestedMappings = $this->metadataFactory->create($value, $nestedPropertyName);

foreach ($nestedMappings as $nestedMapping) {
$nestedTargetPropertyName = $nestedMapping->target ?? $nestedPropertyName;

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

if (false === $nestedMapping->if) {
continue;
}

if (!$nestedProperty->isInitialized($value)) {
continue;
}

$nestedValue = $this->getRawValue($value, $nestedPropertyName);

if ($nestedMapping->if && ($fn = $this->getCallable($nestedMapping->if, $this->conditionCallableLocator))) {
if (!$this->call($fn, $nestedValue, $source, $mappedTarget)) {
continue;
}
}

$nestedValue = $this->getSourceValue($value, $mappedTarget, $nestedValue, $objectMap, $nestedMapping);
$this->storeValue($nestedTargetPropertyName, $mapToProperties, $ctorArguments, $nestedValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not do this, it's already happening at https://github.com/symfony/symfony/blob/7.4/src/Symfony/Component/ObjectMapper/ObjectMapper.php#L231-L261 and in a recursive manner.

}
}
}
}

if ((!$mappingToObject || !$rootCall) && !$map?->transform && $targetConstructor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use Symfony\Component\ObjectMapper\Attribute\Map;

class AddressDto
{
#[Map(target: 'streetAddress')]
public string $street;

#[Map(target: 'city')]
public string $city;

public string $internalCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: BankDataResource::class)]
class BankDataDto
{
#[Map(target: 'iban')]
public string $iban;

public BankDto $bank;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

class BankDataResource
{
public string $iban;
public string $bic;
public string $bankCode;
public string $bankName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use Symfony\Component\ObjectMapper\Attribute\Map;

class BankDto
{
#[Map(target: 'bic')]
public string $bic;

#[Map(target: 'bankCode')]
public string $code;

#[Map(target: 'bankName')]
public string $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: PersonResource::class)]
class PersonDto
{
public string $name;
public AddressDto $address;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

class PersonResource
{
public string $name;
public string $streetAddress;
public string $city;
}
48 changes: 48 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MyProxy;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedObjectMapping\AddressDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedObjectMapping\BankDataDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedObjectMapping\BankDataResource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedObjectMapping\BankDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedObjectMapping\PersonDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedObjectMapping\PersonResource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput\FinalInput;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput\PartialInput;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Source as PromotedConstructorSource;
Expand Down Expand Up @@ -583,4 +589,46 @@ public function testEmbedsAreLazyLoadedByDefault()
$this->assertSame('Test User', $target->user->name);
$this->assertFalse($refl->isUninitializedLazyObject($target->user));
}

public function testNestedObjectMappingWithAttributes()
{
$bankDto = new BankDto();
$bankDto->bic = 'ABCDEFGH';
$bankDto->code = '12345678';
$bankDto->name = 'Test Bank';

$bankDataDto = new BankDataDto();
$bankDataDto->iban = 'DE89370400440532013000';
$bankDataDto->bank = $bankDto;

$mapper = new ObjectMapper();
$result = $mapper->map($bankDataDto, BankDataResource::class);

$this->assertInstanceOf(BankDataResource::class, $result);
$this->assertSame('DE89370400440532013000', $result->iban);
$this->assertSame('ABCDEFGH', $result->bic);
$this->assertSame('12345678', $result->bankCode);
$this->assertSame('Test Bank', $result->bankName);
}

public function testNestedObjectMappingOnlyMapsMappedProperties()
{
$addressDto = new AddressDto();
$addressDto->street = '123 Main St';
$addressDto->city = 'Springfield';
$addressDto->internalCode = 'INTERNAL123';

$personDto = new PersonDto();
$personDto->name = 'John Doe';
$personDto->address = $addressDto;

$mapper = new ObjectMapper();
$result = $mapper->map($personDto);

$this->assertInstanceOf(PersonResource::class, $result);
$this->assertSame('John Doe', $result->name);
$this->assertSame('123 Main St', $result->streetAddress);
$this->assertSame('Springfield', $result->city);
$this->assertObjectNotHasProperty('internalCode', $result);
}
}
Loading