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
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