|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Manager; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\DeserializerInterface; |
|
8
|
|
|
use Chubbyphp\Deserialization\Transformer\TransformerException; |
|
9
|
|
|
use Chubbyphp\Deserialization\TransformerInterface; |
|
10
|
|
|
use Negotiation\Accept as ContentAccept; |
|
11
|
|
|
use Negotiation\Negotiator as ContentNegotiator; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
13
|
|
|
|
|
14
|
|
|
final class RequestManager implements RequestManagerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ContentNegotiator |
|
18
|
|
|
*/ |
|
19
|
|
|
private $contentNegotiator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var DeserializerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $deserializer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var TransformerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $transformer; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param ContentNegotiator $contentNegotiator |
|
33
|
|
|
* @param DeserializerInterface $deserializer |
|
34
|
|
|
* @param TransformerInterface $transformer |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
ContentNegotiator $contentNegotiator, |
|
38
|
|
|
DeserializerInterface $deserializer, |
|
39
|
|
|
TransformerInterface $transformer |
|
40
|
|
|
) { |
|
41
|
|
|
$this->contentNegotiator = $contentNegotiator; |
|
42
|
|
|
$this->deserializer = $deserializer; |
|
43
|
|
|
$this->transformer = $transformer; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Request $request |
|
48
|
|
|
* |
|
49
|
|
|
* @return string|null |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getAccept(Request $request) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->negotiate($request, 'Accept'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* |
|
59
|
|
|
* @return string|null |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getContentType(Request $request) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->negotiate($request, 'Content-Type'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Request $request |
|
68
|
|
|
* @param string $headerName |
|
69
|
|
|
* |
|
70
|
|
|
* @return null|string |
|
71
|
|
|
*/ |
|
72
|
|
|
private function negotiate(Request $request, string $headerName) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$request->hasHeader($headerName)) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** @var ContentAccept $best */ |
|
79
|
|
|
$best = $this->contentNegotiator->getBest( |
|
80
|
|
|
$request->getHeaderLine($headerName), |
|
81
|
|
|
$this->transformer->getContentTypes() |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if (null === $best) { |
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $best->getNormalizedValue(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Request $request |
|
93
|
|
|
* @param object|string $object |
|
94
|
|
|
* |
|
95
|
|
|
* @return object|null |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getDataFromRequestBody(Request $request, $object) |
|
98
|
|
|
{ |
|
99
|
|
|
if (null === $contentType = $this->getContentType($request)) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
$data = $this->transformer->transform((string) $request->getBody(), $contentType); |
|
105
|
|
|
} catch (TransformerException $e) { |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$method = $this->getSerializerMethod($object); |
|
110
|
|
|
|
|
111
|
|
|
return $this->deserializer->$method($data, $object); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param Request $request |
|
116
|
|
|
* @param object|string $object |
|
117
|
|
|
* |
|
118
|
|
|
* @return object|null |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getDataFromRequestQuery(Request $request, $object) |
|
121
|
|
|
{ |
|
122
|
|
|
$method = $this->getSerializerMethod($object); |
|
123
|
|
|
|
|
124
|
|
|
return $this->deserializer->$method($request->getQueryParams(), $object); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param object|string $object |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getSerializerMethod($object): string |
|
133
|
|
|
{ |
|
134
|
|
|
return is_string($object) ? 'deserializeByClass' : 'deserializeByObject'; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|