Skip to content

[ObjectMapper] Add ObjectMapperAwareInterface to set the owning object mapper instance #61145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
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
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
=========

7.4
---

* Add `ObjectMapperAwareInterface` to set the owning object mapper instance

7.3
---

Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @author Antoine Bluchet <soyuka@gmail.com>
*/
final class ObjectMapper implements ObjectMapperInterface
final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface
{
/**
* Tracks recursive references.
Expand All @@ -40,6 +40,7 @@ public function __construct(
private readonly ?PropertyAccessorInterface $propertyAccessor = null,
private readonly ?ContainerInterface $transformCallableLocator = null,
private readonly ?ContainerInterface $conditionCallableLocator = null,
private ?ObjectMapperInterface $objectMapper = null,
) {
}

Expand Down Expand Up @@ -211,7 +212,7 @@ private function getSourceValue(object $source, object $target, mixed $value, \S
} elseif ($objectMap->contains($value)) {
$value = $objectMap[$value];
} else {
$value = $this->map($value, $mapTo->target);
$value = ($this->objectMapper ?? $this)->map($value, $mapTo->target);
}
}

Expand Down Expand Up @@ -327,4 +328,12 @@ private function getSourceReflectionClass(object $source, \ReflectionClass $targ

return $targetRefl;
}

public function withObjectMapper(ObjectMapperInterface $objectMapper): static
{
$clone = clone $this;
$clone->objectMapper = $objectMapper;

return $clone;
}
}
25 changes: 25 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapperAwareInterface.php
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;

/**
* @experimental
*
* @author Antoine Bluchet <soyuka@gmail.com>
*/
interface ObjectMapperAwareInterface
{
/**
* Sets the owning ObjectMapper object.
*/
public function withObjectMapper(ObjectMapperInterface $objectMapper): static;
}
42 changes: 42 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,46 @@ public static function objectMapperProvider(): iterable
yield [new ObjectMapper()];
yield [new ObjectMapper(new ReflectionObjectMapperMetadataFactory(), PropertyAccess::createPropertyAccessor())];
}

public function testDecorateObjectMapper()
{
$mapper = new ObjectMapper();
$myMapper = new class($mapper) implements ObjectMapperInterface {
private ?\SplObjectStorage $embededMap = null;

public function __construct(private readonly ObjectMapperInterface $mapper)
{
$this->embededMap = new \SplObjectStorage();
}

public function map(object $source, object|string|null $target = null): object
{
if (isset($this->embededMap[$source])) {
$target = $this->embededMap[$source];
}

$mapped = $this->mapper->map($source, $target);
$this->embededMap[$source] = $mapped;

return $mapped;
}
};

$mapper = $mapper->withObjectMapper($myMapper);

$d = new D(baz: 'foo', bat: 'bar');
$c = new C(foo: 'foo', bar: 'bar');
$myNewD = $myMapper->map($c);

$a = new A();
$a->foo = 'test';
$a->transform = 'test';
$a->baz = 'me';
$a->notinb = 'test';
$a->relation = $c;
$a->relationNotMapped = $d;

$b = $mapper->map($a);
$this->assertSame($myNewD, $b->relation);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy