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
8 changes: 8 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ public function map(object $source, object|string|null $target = null): object
}
}

if ($mappingToObject && $ctorArguments) {
foreach ($ctorArguments as $property => $value) {
if ($targetRefl->hasProperty($property) && $targetRefl->getProperty($property)->isPublic()) {
$mapToProperties[$property] = $value;
}
}
}

foreach ($mapToProperties as $property => $value) {
$this->propertyAccessor ? $this->propertyAccessor->setValue($mappedTarget, $property, $value) : ($mappedTarget->{$property} = $value);
}
Expand Down
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 <[email protected]>
*
* 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\PromotedConstructor;

class Source
{
public function __construct(
public int $id,
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 <[email protected]>
*
* 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\PromotedConstructor;

class Target
{
public function __construct(
public int $id,
public string $name,
) {
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory;
use Symfony\Component\ObjectMapper\ObjectMapper;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\ObjectMapper\Tests\Fixtures\A;
use Symfony\Component\ObjectMapper\Tests\Fixtures\B;
use Symfony\Component\ObjectMapper\Tests\Fixtures\C;
Expand Down Expand Up @@ -51,6 +52,8 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\C as MultipleTargetPropertyC;
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\PromotedConstructor\Source as PromotedConstructorSource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\Dto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\A as ServiceLocatorA;
Expand Down Expand Up @@ -345,4 +348,24 @@ public function testDefaultValueStdClassWithPropertyInfo()
$this->assertSame('abc', $b->id);
$this->assertNull($b->optional);
}

/**
* @dataProvider objectMapperProvider
*/
public function testUpdateObjectWithConstructorPromotedProperties(ObjectMapperInterface $mapper)
{
$a = new PromotedConstructorSource(1, 'foo');
$b = new PromotedConstructorTarget(1, 'bar');
$v = $mapper->map($a, $b);
$this->assertSame($v->name, 'foo');
}

/**
* @return iterable<array{0: ObjectMapperInterface}>
*/
public static function objectMapperProvider(): iterable
{
yield [new ObjectMapper()];
yield [new ObjectMapper(new ReflectionObjectMapperMetadataFactory(), PropertyAccess::createPropertyAccessor())];
}
}
Loading