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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
46 changes: 31 additions & 15 deletions src/State/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the if should be the other way around. The way it is right now you're including ObjectMapperAwareInterface if it doesn't exist and not if it does, which obviously doesn't make sense.

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;
}
}
}
5 changes: 4 additions & 1 deletion src/State/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\\": ""
Expand Down
Loading