This repository was archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResponseManager.php
More file actions
105 lines (83 loc) · 2.93 KB
/
ResponseManager.php
File metadata and controls
105 lines (83 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Chubbyphp\ApiHttp\Manager;
use Chubbyphp\ApiHttp\ApiProblem\ApiProblemInterface;
use Chubbyphp\Deserialization\DeserializerInterface;
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
use Chubbyphp\Serialization\SerializerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
final class ResponseManager implements ResponseManagerInterface
{
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct()
{
$args = func_get_args();
if ($args[0] instanceof DeserializerInterface) {
@trigger_error(
'Remove deserializer as first argument.',
E_USER_DEPRECATED
);
array_shift($args);
}
$this->setResponseFactory($args[0]);
$this->setSerializer($args[1]);
}
/**
* @param object $object
* @param string $accept
* @param int $status
* @param NormalizerContextInterface|null $context
* @return ResponseInterface
*/
public function create(
$object,
string $accept,
int $status = 200,
?NormalizerContextInterface $context = null
): ResponseInterface {
$body = $this->serializer->serialize($object, $accept, $context);
$response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
$response->getBody()->write($body);
return $response;
}
public function createEmpty(string $accept, int $status = 204): ResponseInterface
{
return $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
}
public function createRedirect(string $location, int $status = 307): ResponseInterface
{
return $this->responseFactory->createResponse($status)->withHeader('Location', $location);
}
public function createFromApiProblem(
ApiProblemInterface $apiProblem,
string $accept,
?NormalizerContextInterface $context = null
): ResponseInterface {
$status = $apiProblem->getStatus();
$response = $this->responseFactory->createResponse($status)
->withHeader('Content-Type', str_replace('/', '/problem+', $accept))
;
foreach ($apiProblem->getHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
}
$body = $this->serializer->serialize($apiProblem, $accept, $context);
$response->getBody()->write($body);
return $response;
}
private function setResponseFactory(ResponseFactoryInterface $responseFactory): void
{
$this->responseFactory = $responseFactory;
}
private function setSerializer(SerializerInterface $serializer): void
{
$this->serializer = $serializer;
}
}