diff --git a/composer.json b/composer.json index 14c4d705c4..a5c43b2aed 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ "doctrine/mongodb-odm": "<2.4", "doctrine/persistence": "<1.3", "symfony/framework-bundle": "6.4.6 || 7.0.6", + "symfony/object-mapper": "<7.3.4", "symfony/var-exporter": "<6.1.1", "phpunit/phpunit": "<9.5", "phpspec/prophecy": "<1.15" @@ -183,7 +184,7 @@ "symfony/maker-bundle": "^1.24", "symfony/mercure-bundle": "*", "symfony/messenger": "^6.4 || ^7.0", - "symfony/object-mapper": "7.4.x-dev", + "symfony/object-mapper": "^7.3 || 7.4.x-dev", "symfony/routing": "^6.4 || ^7.0", "symfony/security-bundle": "^6.4 || ^7.0", "symfony/security-core": "^6.4 || ^7.0", diff --git a/src/State/ObjectMapper/ObjectMapper.php b/src/State/ObjectMapper/ObjectMapper.php index ff4909c96c..e988280645 100644 --- a/src/State/ObjectMapper/ObjectMapper.php +++ b/src/State/ObjectMapper/ObjectMapper.php @@ -17,28 +17,44 @@ use Symfony\Component\ObjectMapper\ObjectMapperAwareInterface; use Symfony\Component\ObjectMapper\ObjectMapperInterface; -final class ObjectMapper implements ObjectMapperInterface -{ - public function __construct(private ObjectMapperInterface $decorated) +if (class_exists(ObjectMapperAwareInterface::class)) { + final class ObjectMapper implements ObjectMapperInterface { - if ($this->decorated instanceof ObjectMapperAwareInterface) { - $this->decorated = $this->decorated->withObjectMapper($this); + public function __construct(private ObjectMapperInterface $decorated) + { } - } - public function map(object $source, object|string|null $target = null): object - { - return $this->decorated->map($source, $target); + public function map(object $source, object|string|null $target = null): object + { + return $this->decorated->map($source, $target); + } } - - public function withObjectMapper(ObjectMapperInterface $objectMapper): static +} else { + final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface { - if (!$this->decorated instanceof ObjectMapperAwareInterface) { - throw new RuntimeException(\sprintf('Given object mapper "%s" does not implements %s.', get_debug_type($this->decorated), ObjectMapperAwareInterface::class)); + public function __construct(private ObjectMapperInterface $decorated) + { + if ($this->decorated instanceof ObjectMapperAwareInterface) { + $this->decorated = $this->decorated->withObjectMapper($this); + } } - $this->decorated->withObjectMapper($objectMapper); + public function map(object $source, object|string|null $target = null): object + { + return $this->decorated->map($source, $target); + } - return $this; + public function withObjectMapper(ObjectMapperInterface $objectMapper): static + { + $s = clone $this; + + if (!$s->decorated instanceof ObjectMapperAwareInterface) { + throw new RuntimeException(\sprintf('Given object mapper "%s" does not implements %s.', get_debug_type($this->decorated), ObjectMapperAwareInterface::class)); + } + + $s->decorated->withObjectMapper($objectMapper); + + return $s; + } } } diff --git a/src/State/composer.json b/src/State/composer.json index 1484f26114..97df017495 100644 --- a/src/State/composer.json +++ b/src/State/composer.json @@ -40,10 +40,13 @@ "phpunit/phpunit": "11.5.x-dev", "symfony/http-foundation": "^6.4 || ^7.0", "symfony/object-mapper": "^7.3", - "symfony/type-info": "^7.3", + "symfony/type-info": "^7.3 || 7.4.x-dev", "symfony/web-link": "^6.4 || ^7.1", "willdurand/negotiation": "^3.1" }, + "conflicts": { + "symfony/object-mapper": "<7.3.4" + }, "autoload": { "psr-4": { "ApiPlatform\\State\\": ""