Skip to content

Commit 98fc352

Browse files
authored
fix(symfony): object mapper compatibility (#7531)
fixes #7504
1 parent a5c4462 commit 98fc352

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"doctrine/mongodb-odm": "<2.4",
4040
"doctrine/persistence": "<1.3",
4141
"symfony/framework-bundle": "6.4.6 || 7.0.6",
42+
"symfony/object-mapper": "<7.3.4",
4243
"symfony/var-exporter": "<6.1.1",
4344
"phpunit/phpunit": "<9.5",
4445
"phpspec/prophecy": "<1.15"
@@ -183,7 +184,7 @@
183184
"symfony/maker-bundle": "^1.24",
184185
"symfony/mercure-bundle": "*",
185186
"symfony/messenger": "^6.4 || ^7.0",
186-
"symfony/object-mapper": "7.4.x-dev",
187+
"symfony/object-mapper": "^7.3 || 7.4.x-dev",
187188
"symfony/routing": "^6.4 || ^7.0",
188189
"symfony/security-bundle": "^6.4 || ^7.0",
189190
"symfony/security-core": "^6.4 || ^7.0",

src/State/ObjectMapper/ObjectMapper.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,44 @@
1717
use Symfony\Component\ObjectMapper\ObjectMapperAwareInterface;
1818
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
1919

20-
final class ObjectMapper implements ObjectMapperInterface
21-
{
22-
public function __construct(private ObjectMapperInterface $decorated)
20+
if (class_exists(ObjectMapperAwareInterface::class)) {
21+
final class ObjectMapper implements ObjectMapperInterface
2322
{
24-
if ($this->decorated instanceof ObjectMapperAwareInterface) {
25-
$this->decorated = $this->decorated->withObjectMapper($this);
23+
public function __construct(private ObjectMapperInterface $decorated)
24+
{
2625
}
27-
}
2826

29-
public function map(object $source, object|string|null $target = null): object
30-
{
31-
return $this->decorated->map($source, $target);
27+
public function map(object $source, object|string|null $target = null): object
28+
{
29+
return $this->decorated->map($source, $target);
30+
}
3231
}
33-
34-
public function withObjectMapper(ObjectMapperInterface $objectMapper): static
32+
} else {
33+
final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface
3534
{
36-
if (!$this->decorated instanceof ObjectMapperAwareInterface) {
37-
throw new RuntimeException(\sprintf('Given object mapper "%s" does not implements %s.', get_debug_type($this->decorated), ObjectMapperAwareInterface::class));
35+
public function __construct(private ObjectMapperInterface $decorated)
36+
{
37+
if ($this->decorated instanceof ObjectMapperAwareInterface) {
38+
$this->decorated = $this->decorated->withObjectMapper($this);
39+
}
3840
}
3941

40-
$this->decorated->withObjectMapper($objectMapper);
42+
public function map(object $source, object|string|null $target = null): object
43+
{
44+
return $this->decorated->map($source, $target);
45+
}
4146

42-
return $this;
47+
public function withObjectMapper(ObjectMapperInterface $objectMapper): static
48+
{
49+
$s = clone $this;
50+
51+
if (!$s->decorated instanceof ObjectMapperAwareInterface) {
52+
throw new RuntimeException(\sprintf('Given object mapper "%s" does not implements %s.', get_debug_type($this->decorated), ObjectMapperAwareInterface::class));
53+
}
54+
55+
$s->decorated->withObjectMapper($objectMapper);
56+
57+
return $s;
58+
}
4359
}
4460
}

src/State/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@
4040
"phpunit/phpunit": "11.5.x-dev",
4141
"symfony/http-foundation": "^6.4 || ^7.0",
4242
"symfony/object-mapper": "^7.3",
43-
"symfony/type-info": "^7.3",
43+
"symfony/type-info": "^7.3 || 7.4.x-dev",
4444
"symfony/web-link": "^6.4 || ^7.1",
4545
"willdurand/negotiation": "^3.1"
4646
},
47+
"conflicts": {
48+
"symfony/object-mapper": "<7.3.4"
49+
},
4750
"autoload": {
4851
"psr-4": {
4952
"ApiPlatform\\State\\": ""

0 commit comments

Comments
 (0)