|
17 | 17 | use Symfony\Component\ObjectMapper\ObjectMapperAwareInterface; |
18 | 18 | use Symfony\Component\ObjectMapper\ObjectMapperInterface; |
19 | 19 |
|
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 |
23 | 22 | { |
24 | | - if ($this->decorated instanceof ObjectMapperAwareInterface) { |
25 | | - $this->decorated = $this->decorated->withObjectMapper($this); |
| 23 | + public function __construct(private ObjectMapperInterface $decorated) |
| 24 | + { |
26 | 25 | } |
27 | | - } |
28 | 26 |
|
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 | + } |
32 | 31 | } |
33 | | - |
34 | | - public function withObjectMapper(ObjectMapperInterface $objectMapper): static |
| 32 | +} else { |
| 33 | + final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface |
35 | 34 | { |
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 | + } |
38 | 40 | } |
39 | 41 |
|
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 | + } |
41 | 46 |
|
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 | + } |
43 | 59 | } |
44 | 60 | } |
0 commit comments