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
5 changes: 5 additions & 0 deletions src/Symfony/Component/ObjectMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

8.0
---

* Merge nested properties when targeting the same class

7.4
---

Expand Down
28 changes: 22 additions & 6 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,30 @@ private function doMap(object $source, object|string|null $target, \WeakMap $obj
$this->storeValue($targetPropertyName, $mapToProperties, $ctorArguments, $value);
}

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

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

$value = $this->getSourceValue($source, $mappedTarget, $this->getRawValue($source, $propertyName), $objectMap);
$rawValue = $this->getRawValue($source, $propertyName);
if ($targetRefl->hasProperty($propertyName)) {
$value = $this->getSourceValue($source, $mappedTarget, $rawValue, $objectMap);
$this->storeValue($propertyName, $mapToProperties, $ctorArguments, $value);
continue;
}

if (
\is_object($rawValue)
&& ($innerMetadata = $this->metadataFactory->create($rawValue))
&& ($mapTo = $this->getMapTarget($innerMetadata, $rawValue, $source, $mappedTarget))
&& \is_string($mapTo->target)
&& $mapTo->target === $targetRefl->getName()
) {
($this->objectMapper ?? $this)->map($rawValue, $mappedTarget);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\NestedMapping;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: NestedBankDataResource::class)]
class NestedBankDataDto
{
public string $iban;
public NestedBankDto $bank;
}

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\NestedMapping;

class NestedBankDataResource
{
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,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\NestedMapping;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: NestedBankDataResource::class)]
class NestedBankDto
{
public string $bic;
#[Map(target: 'bankCode')]
public string $code;
#[Map(target: 'bankName')]
public string $name;
}

24 changes: 24 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
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\NestedMapping\NestedBankDataDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedMapping\NestedBankDataResource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\NestedMapping\NestedBankDto;
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 @@ -651,4 +654,25 @@ public function testBugReportLazyLoadingPromotedReadonlyProperty()
$this->assertSame('foo', $out->var1);
$this->assertSame('bar', $out->b->var2);
}

public function testNestedBankDataMapping()
{
$bankDto = new NestedBankDto();
$bankDto->bic = 'BIC123';
$bankDto->code = 'BANK001';
$bankDto->name = 'Test Bank';

$bankDataDto = new NestedBankDataDto();
$bankDataDto->iban = 'IBAN12345';
$bankDataDto->bank = $bankDto;

$mapper = new ObjectMapper();
$bankDataResource = $mapper->map($bankDataDto, NestedBankDataResource::class);

$this->assertInstanceOf(NestedBankDataResource::class, $bankDataResource);
$this->assertSame('IBAN12345', $bankDataResource->iban);
$this->assertSame('BIC123', $bankDataResource->bic);
$this->assertSame('BANK001', $bankDataResource->bankCode);
$this->assertSame('Test Bank', $bankDataResource->bankName);
}
}
Loading