From d6ff8d1d77a6d7f98c1f3579e8de8677a2f43744 Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 17 Jul 2025 12:06:22 +0200 Subject: [PATCH] [ObjectMapper] allow owning ObjectMapper object --- .../Component/ObjectMapper/CHANGELOG.md | 5 +++ .../Component/ObjectMapper/ObjectMapper.php | 13 +++++- .../ObjectMapperAwareInterface.php | 25 +++++++++++ .../ObjectMapper/Tests/ObjectMapperTest.php | 42 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/ObjectMapper/ObjectMapperAwareInterface.php diff --git a/src/Symfony/Component/ObjectMapper/CHANGELOG.md b/src/Symfony/Component/ObjectMapper/CHANGELOG.md index 0f29770616c5f..efb8cf7554ee8 100644 --- a/src/Symfony/Component/ObjectMapper/CHANGELOG.md +++ b/src/Symfony/Component/ObjectMapper/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Add `ObjectMapperAwareInterface` to set the owning object mapper instance + 7.3 --- diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapper.php b/src/Symfony/Component/ObjectMapper/ObjectMapper.php index 28eda13513119..f233d8e66384f 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapper.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapper.php @@ -28,7 +28,7 @@ * * @author Antoine Bluchet */ -final class ObjectMapper implements ObjectMapperInterface +final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface { /** * Tracks recursive references. @@ -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, ) { } @@ -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); } } @@ -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; + } } diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapperAwareInterface.php b/src/Symfony/Component/ObjectMapper/ObjectMapperAwareInterface.php new file mode 100644 index 0000000000000..1041cadfc3849 --- /dev/null +++ b/src/Symfony/Component/ObjectMapper/ObjectMapperAwareInterface.php @@ -0,0 +1,25 @@ + + * + * 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 + */ +interface ObjectMapperAwareInterface +{ + /** + * Sets the owning ObjectMapper object. + */ + public function withObjectMapper(ObjectMapperInterface $objectMapper): static; +} diff --git a/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php b/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php index 81c1d56b17f6b..e5c9643c39152 100644 --- a/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php +++ b/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php @@ -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); + } } 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