diff --git a/src/State/ObjectMapper/ObjectMapper.php b/src/State/ObjectMapper/ObjectMapper.php index b089891e3f6..187eae7dd6e 100644 --- a/src/State/ObjectMapper/ObjectMapper.php +++ b/src/State/ObjectMapper/ObjectMapper.php @@ -13,10 +13,11 @@ namespace ApiPlatform\State\ObjectMapper; +use ApiPlatform\Metadata\Exception\RuntimeException; use Symfony\Component\ObjectMapper\ObjectMapperAwareInterface; use Symfony\Component\ObjectMapper\ObjectMapperInterface; -final class ObjectMapper implements ObjectMapperInterface, ClearObjectMapInterface +final class ObjectMapper implements ObjectMapperInterface, ClearObjectMapInterface, ObjectMapperAwareInterface { private ?\SplObjectStorage $objectMap = null; @@ -48,4 +49,15 @@ public function clearObjectMap(): void $this->objectMap->detach($k); } } + + public function withObjectMapper(ObjectMapperInterface $objectMapper): static + { + if (!$this->decorated instanceof ObjectMapperAwareInterface) { + throw new RuntimeException(\sprintf('Given object mapper "%s" does not implements %s.', get_debug_type($this->decorated), ObjectMapperAwareInterface::class)); + } + + $this->decorated->withObjectMapper($objectMapper); + + return $this; + } }