|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace W2w\Lib\Apie\Serializers; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
9
|
|
|
use W2w\Lib\Apie\Encodings\FormatRetrieverInterface; |
|
10
|
|
|
use Zend\Diactoros\Response\TextResponse; |
|
11
|
|
|
|
|
12
|
|
|
class SymfonySerializerAdapter implements ResourceSerializerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private $serializer; |
|
15
|
|
|
|
|
16
|
|
|
private $formatRetriever; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Serializer $serializer, FormatRetrieverInterface $formatRetriever) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->serializer = $serializer; |
|
21
|
|
|
$this->formatRetriever = $formatRetriever; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function putData(object $resource, string $requestBody, string $contentType): object |
|
28
|
|
|
{ |
|
29
|
|
|
$contentFormat = $this->formatRetriever->getFormat($contentType); |
|
30
|
|
|
return $this->serializer->deserialize( |
|
|
|
|
|
|
31
|
|
|
$requestBody, |
|
32
|
|
|
get_class($resource), |
|
33
|
|
|
$contentFormat, |
|
|
|
|
|
|
34
|
|
|
[ |
|
35
|
|
|
'groups' => ['base', 'write', 'put'], |
|
36
|
|
|
'object_to_populate' => $resource |
|
37
|
|
|
] |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function postData(string $resourceClass, string $requestBody, string $contentType): object |
|
45
|
|
|
{ |
|
46
|
|
|
$contentFormat = $this->formatRetriever->getFormat($contentType); |
|
47
|
|
|
return $this->serializer->deserialize( |
|
|
|
|
|
|
48
|
|
|
$requestBody, |
|
49
|
|
|
$resourceClass, |
|
50
|
|
|
$contentFormat, |
|
|
|
|
|
|
51
|
|
|
[ |
|
52
|
|
|
'groups' => ['base', 'write', 'post'], |
|
53
|
|
|
] |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function toResponse(?object $resource, string $acceptHeader): ResponseInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$format = $this->formatRetriever->getFormat($acceptHeader ?? 'application/json'); |
|
63
|
|
|
$contentType = $this->formatRetriever->getContentType($format); |
|
|
|
|
|
|
64
|
|
|
$response = $this->serializer->serialize($this->resource, $format, ['groups' => ['base', 'read', 'get']]); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
return new TextResponse($response, is_null($resource) ? 204 : 200, ['content-type' => $contentType]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritDoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function normalize(?object $resource, string $acceptHeader) |
|
73
|
|
|
{ |
|
74
|
|
|
$format = $this->formatRetriever->getFormat($acceptHeader ?? 'application/json'); |
|
75
|
|
|
return $this->serializer->normalize($this->resource, $format, ['groups' => ['base', 'read', 'get']]); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|