1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
19
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
20
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ApiNormalizer |
26
|
|
|
{ |
27
|
|
|
private NormalizerInterface $decorated; |
28
|
|
|
private EntityManagerInterface $entityManager; |
29
|
|
|
|
30
|
|
|
public function __construct(NormalizerInterface $decorated, EntityManagerInterface $entityManager) |
31
|
|
|
{ |
32
|
|
|
if (!$decorated instanceof DenormalizerInterface) { |
33
|
|
|
throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->decorated = $decorated; |
37
|
|
|
$this->entityManager = $entityManager; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function supportsNormalization($data, $format = null): bool |
41
|
|
|
{ |
42
|
|
|
dump($data, $format, $this->decorated->supportsNormalization($data, $format)); |
43
|
|
|
|
44
|
|
|
return $this->decorated->supportsNormalization($data, $format); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function normalize($object, $format = null, array $context = []) |
48
|
|
|
{ |
49
|
|
|
$data = $this->decorated->normalize($object, $format, $context); |
50
|
|
|
$data['__persisted__'] = $this->entityManager->contains($object); |
51
|
|
|
dump($data); |
52
|
|
|
|
53
|
|
|
return $data; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function supportsDenormalization($data, $type, $format = null): bool |
57
|
|
|
{ |
58
|
|
|
return $this->decorated->supportsDenormalization($data, $type, $format); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
62
|
|
|
{ |
63
|
|
|
return $this->decorated->denormalize($data, $class, $format, $context); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setSerializer(SerializerInterface $serializer): void |
67
|
|
|
{ |
68
|
|
|
if ($this->decorated instanceof SerializerAwareInterface) { |
69
|
|
|
$this->decorated->setSerializer($serializer); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|