diff --git a/src/Symfony/Component/ObjectMapper/CHANGELOG.md b/src/Symfony/Component/ObjectMapper/CHANGELOG.md index 0f29770616c5..efb8cf7554ee 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 28eda1351311..f233d8e66384 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 000000000000..1041cadfc384 --- /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 81c1d56b17f6..e5c9643c3915 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