1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Manager; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\ApiHttp\ApiProblem\ApiProblemInterface; |
8
|
|
|
use Chubbyphp\Deserialization\DeserializerInterface; |
9
|
|
|
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; |
10
|
|
|
use Chubbyphp\Serialization\SerializerInterface; |
11
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
final class ResponseManager implements ResponseManagerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DeserializerInterface |
18
|
|
|
*/ |
19
|
|
|
private $deserializer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ResponseFactoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $responseFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var SerializerInterface |
28
|
|
|
*/ |
29
|
|
|
private $serializer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DeserializerInterface $deserializer |
33
|
|
|
* @param ResponseFactoryInterface $responseFactory |
34
|
|
|
* @param SerializerInterface $serializer |
35
|
|
|
*/ |
36
|
7 |
|
public function __construct( |
37
|
|
|
DeserializerInterface $deserializer, |
38
|
|
|
ResponseFactoryInterface $responseFactory, |
39
|
|
|
SerializerInterface $serializer |
40
|
|
|
) { |
41
|
7 |
|
$this->deserializer = $deserializer; |
42
|
7 |
|
$this->responseFactory = $responseFactory; |
43
|
7 |
|
$this->serializer = $serializer; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param object $object |
48
|
|
|
* @param string $accept |
49
|
|
|
* @param int $status |
50
|
|
|
* @param NormalizerContextInterface|null $context |
51
|
|
|
* |
52
|
|
|
* @return ResponseInterface |
53
|
|
|
*/ |
54
|
2 |
|
public function create( |
55
|
|
|
$object, |
56
|
|
|
string $accept, |
57
|
|
|
int $status = 200, |
58
|
|
|
NormalizerContextInterface $context = null |
59
|
|
|
): ResponseInterface { |
60
|
2 |
|
$body = $this->serializer->serialize($object, $accept, $context); |
61
|
|
|
|
62
|
2 |
|
$response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept); |
63
|
2 |
|
$response->getBody()->write($body); |
64
|
|
|
|
65
|
2 |
|
return $response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $accept |
70
|
|
|
* @param int $status |
71
|
|
|
* |
72
|
|
|
* @return ResponseInterface |
73
|
|
|
*/ |
74
|
2 |
|
public function createEmpty(string $accept, int $status = 204): ResponseInterface |
75
|
|
|
{ |
76
|
2 |
|
return $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $location |
81
|
|
|
* @param int $status |
82
|
|
|
* |
83
|
|
|
* @return ResponseInterface |
84
|
|
|
*/ |
85
|
2 |
|
public function createRedirect(string $location, int $status = 307): ResponseInterface |
86
|
|
|
{ |
87
|
2 |
|
return $this->responseFactory->createResponse($status)->withHeader('Location', $location); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ApiProblemInterface $apiProblem |
92
|
|
|
* @param string $accept |
93
|
|
|
* @param NormalizerContextInterface $context |
94
|
|
|
* |
95
|
|
|
* @return ResponseInterface |
96
|
|
|
*/ |
97
|
1 |
|
public function createFromApiProblem( |
98
|
|
|
ApiProblemInterface $apiProblem, |
99
|
|
|
string $accept, |
100
|
|
|
NormalizerContextInterface $context = null |
101
|
|
|
): ResponseInterface { |
102
|
1 |
|
$status = $apiProblem->getStatus(); |
103
|
|
|
|
104
|
1 |
|
$response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept); |
105
|
|
|
|
106
|
1 |
|
foreach ($apiProblem->getHeaders() as $name => $value) { |
107
|
1 |
|
$response = $response->withHeader($name, $value); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$body = $this->serializer->serialize($apiProblem, $accept, $context); |
111
|
|
|
|
112
|
1 |
|
$response->getBody()->write($body); |
113
|
|
|
|
114
|
1 |
|
return $response; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|