1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Plugins\Core\Serializers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Symfony\Component\Serializer\Serializer; |
7
|
|
|
use W2w\Lib\Apie\Interfaces\FormatRetrieverInterface; |
8
|
|
|
use W2w\Lib\Apie\Interfaces\ResourceSerializerInterface; |
9
|
|
|
use W2w\Lib\Apie\Plugins\Core\Normalizers\ContextualNormalizer; |
10
|
|
|
use W2w\Lib\Apie\Plugins\Core\Normalizers\EvilReflectionPropertyNormalizer; |
11
|
|
|
use Zend\Diactoros\Response\TextResponse; |
12
|
|
|
|
13
|
|
|
class SymfonySerializerAdapter implements ResourceSerializerInterface |
14
|
|
|
{ |
15
|
|
|
private $serializer; |
16
|
|
|
|
17
|
|
|
private $formatRetriever; |
18
|
|
|
|
19
|
|
|
public function __construct(Serializer $serializer, FormatRetrieverInterface $formatRetriever) |
20
|
|
|
{ |
21
|
|
|
$this->serializer = $serializer; |
22
|
|
|
$this->formatRetriever = $formatRetriever; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* In case the symfony serializer is needed outside the adapter. It's highly discouraged to use |
27
|
|
|
* directly. |
28
|
|
|
* |
29
|
|
|
* @return Serializer |
30
|
|
|
*/ |
31
|
|
|
public function getSerializer(): Serializer |
32
|
|
|
{ |
33
|
|
|
return $this->serializer; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
|
|
public function putData(object $resource, string $requestBody, string $contentType): object |
40
|
|
|
{ |
41
|
|
|
$contentFormat = $this->formatRetriever->getFormat($contentType) ?? 'json'; |
42
|
|
|
return $this->serializer->deserialize( |
|
|
|
|
43
|
|
|
$requestBody, |
44
|
|
|
get_class($resource), |
45
|
|
|
$contentFormat, |
46
|
|
|
[ |
47
|
|
|
'groups' => ['base', 'write', 'put'], |
48
|
|
|
'object_to_populate' => $resource |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function postData(string $resourceClass, string $requestBody, string $contentType): object |
57
|
|
|
{ |
58
|
|
|
$contentFormat = $this->formatRetriever->getFormat($contentType) ?? 'json'; |
59
|
|
|
return $this->serializer->deserialize( |
|
|
|
|
60
|
|
|
$requestBody, |
61
|
|
|
$resourceClass, |
62
|
|
|
$contentFormat, |
63
|
|
|
[ |
64
|
|
|
'groups' => ['base', 'write', 'post'], |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function toResponse($resource, string $acceptHeader): ResponseInterface |
73
|
|
|
{ |
74
|
|
|
$format = $this->formatRetriever->getFormat($acceptHeader) ?? 'json'; |
75
|
|
|
$contentType = $this->formatRetriever->getContentType($format); |
76
|
|
|
$response = $this->serializer->serialize($resource, $format, ['groups' => ['base', 'read', 'get']]); |
77
|
|
|
|
78
|
|
|
return new TextResponse($response, is_null($resource) ? 204 : 200, ['content-type' => $contentType]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
|
|
public function normalize($resource, string $acceptHeader) |
85
|
|
|
{ |
86
|
|
|
$format = $this->formatRetriever->getFormat($acceptHeader); |
87
|
|
|
return $this->serializer->normalize($resource, $format, ['groups' => ['base', 'read', 'get']]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function hydrateWithReflection(array $data, string $resourceClass) |
94
|
|
|
{ |
95
|
|
|
ContextualNormalizer::enableDenormalizer(EvilReflectionPropertyNormalizer::class); |
96
|
|
|
try { |
97
|
|
|
return $this->serializer->denormalize( |
98
|
|
|
$data, |
99
|
|
|
$resourceClass, |
100
|
|
|
null, |
101
|
|
|
['disable_type_enforcement' => true] |
102
|
|
|
); |
103
|
|
|
} finally { |
104
|
|
|
ContextualNormalizer::disableDenormalizer(EvilReflectionPropertyNormalizer::class); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|