🌐 AI搜索 & 代理 主页
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
[ObjectMapper] Fix parameter passed to class level transform
Fixes #60827
  • Loading branch information
mttsch committed Jun 19, 2025
commit 7a3c92f2634a4a3cca2bab5a3803552bbbac0739
2 changes: 1 addition & 1 deletion src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function map(object $source, object|string|null $target = null): object

$mappedTarget = $mappingToObject ? $target : $targetRefl->newInstanceWithoutConstructor();
if ($map && $map->transform) {
$mappedTarget = $this->applyTransforms($map, $mappedTarget, $mappedTarget, null);
$mappedTarget = $this->applyTransforms($map, $mappedTarget, $source, null);

if (!\is_object($mappedTarget)) {
throw new MappingTransformException(\sprintf('Cannot map "%s" to a non-object target of type "%s".', get_debug_type($source), get_debug_type($mappedTarget)));
Expand Down
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\InstanceCallbackWithArguments;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: B::class, transform: [B::class, 'newInstance'])]
class A
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\InstanceCallbackWithArguments;

class B
{
public mixed $transformValue;
public object $transformSource;

public static function newInstance(mixed $value, object $source): self
{
$b = new self();
$b->transformValue = $value;
$b->transformSource = $source;

return $b;
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\HydrateObject\SourceOnly;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\A as InstanceCallbackA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B as InstanceCallbackB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\A as InstanceCallbackWithArgumentsA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\B as InstanceCallbackWithArgumentsB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\AToBMapper;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\MapStructMapperMetadataFactory;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Source;
Expand Down Expand Up @@ -155,6 +157,16 @@ public function testMapToWithInstanceHook()
$this->assertSame($b->name, 'test');
}

public function testMapToWithInstanceHookWithArguments()
{
$a = new InstanceCallbackWithArgumentsA();
$mapper = new ObjectMapper();
$b = $mapper->map($a);
$this->assertInstanceOf(InstanceCallbackWithArgumentsB::class, $b);
$this->assertSame($a, $b->transformSource);
$this->assertInstanceOf(InstanceCallbackWithArgumentsB::class, $b->transformValue);
}

public function testMapStruct()
{
$a = new Source('a', 'b', 'c');
Expand Down
Loading