Skip to content

Commit 7a24bdc

Browse files
committed
bug symfony#60073 mapping on target (reverse-side mapping) (soyuka)
This PR was merged into the 7.3 branch. Discussion ---------- mapping on target (reverse-side mapping) | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#60058 | License | MIT This allows to configure mapping on the target instead of the source: ```php <?php #[Map(target: self::class, source: A::class)] class B { public function __construct(#[Map(source: 'source')] public string $target) { } } ``` Commits ------- c142673 [ObjectMapper] mapping on target (reverse-side mapping)
2 parents 110762f + c142673 commit 7a24bdc

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private function getSourceReflectionClass(object $source, \ReflectionClass $targ
298298
}
299299

300300
foreach ($refl->getProperties() as $property) {
301-
if ($this->metadataFactory->create($source, $property)) {
301+
if ($this->metadataFactory->create($source, $property->getName())) {
302302
return $refl;
303303
}
304304
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\MapTargetToSource;
13+
14+
class A
15+
{
16+
public function __construct(public string $source)
17+
{
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\MapTargetToSource;
13+
14+
use Symfony\Component\ObjectMapper\Attribute\Map;
15+
16+
#[Map(source: A::class)]
17+
class B
18+
{
19+
public function __construct(#[Map(source: 'source')] public string $target)
20+
{
21+
}
22+
}

src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\MapStructMapperMetadataFactory;
3939
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Source;
4040
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Target;
41+
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapTargetToSource\A as MapTargetToSourceA;
42+
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapTargetToSource\B as MapTargetToSourceB;
4143
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
4244
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
4345
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
@@ -262,4 +264,13 @@ public function testTransformToWrongObject()
262264
$mapper = new ObjectMapper($metadata);
263265
$mapper->map($u);
264266
}
267+
268+
public function testMapTargetToSource()
269+
{
270+
$a = new MapTargetToSourceA('str');
271+
$mapper = new ObjectMapper();
272+
$b = $mapper->map($a, MapTargetToSourceB::class);
273+
$this->assertInstanceOf(MapTargetToSourceB::class, $b);
274+
$this->assertSame('str', $b->target);
275+
}
265276
}

0 commit comments

Comments
 (0)