|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Utility\Hash; |
|
6
|
|
|
use Psr\Http\Message\RequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
10
|
|
|
use React\Http\Io\UploadedFile; |
|
11
|
|
|
use RingCentral\Psr7\Request; |
|
12
|
|
|
use RingCentral\Psr7\Response; |
|
13
|
|
|
use RingCentral\Psr7\ServerRequest; |
|
14
|
|
|
use function RingCentral\Psr7\stream_for; |
|
15
|
|
|
|
|
16
|
|
|
function psr7_response_json_encode(ResponseInterface $response): string |
|
17
|
|
|
{ |
|
18
|
5 |
|
return json_try_encode(psr7_response_encode($response)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
function psr7_response_encode(ResponseInterface $response): array |
|
22
|
|
|
{ |
|
23
|
10 |
|
$json = []; |
|
24
|
10 |
|
$json['protocol_version'] = $response->getProtocolVersion(); |
|
25
|
10 |
|
$json['status_code'] = $response->getStatusCode(); |
|
26
|
10 |
|
$json['reason_phrase'] = $response->getReasonPhrase(); |
|
27
|
10 |
|
$json['headers'] = sort_headers($response->getHeaders()); |
|
28
|
10 |
|
$json['body'] = \base64_encode((string)$response->getBody()); |
|
29
|
|
|
|
|
30
|
10 |
|
return $json; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @throws NotAnEncodedResponseException |
|
35
|
|
|
*/ |
|
36
|
|
|
function psr7_response_json_decode(string $json): ResponseInterface |
|
37
|
|
|
{ |
|
38
|
2 |
|
return psr7_response_decode(json_try_decode($json, true)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @throws NotAnEncodedResponseException |
|
43
|
|
|
*/ |
|
44
|
|
|
function psr7_response_decode(array $json): ResponseInterface |
|
45
|
|
|
{ |
|
46
|
|
|
$properties = [ |
|
47
|
4 |
|
'protocol_version', |
|
48
|
|
|
'status_code', |
|
49
|
|
|
'reason_phrase', |
|
50
|
|
|
'headers', |
|
51
|
|
|
'body', |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
4 |
|
validate_array($json, $properties, NotAnEncodedResponseException::class); |
|
55
|
|
|
|
|
56
|
2 |
|
return new Response( |
|
57
|
2 |
|
$json['status_code'], |
|
58
|
2 |
|
$json['headers'], |
|
59
|
2 |
|
\base64_decode($json['body'], true), |
|
60
|
2 |
|
$json['protocol_version'], |
|
61
|
2 |
|
$json['reason_phrase'] |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
function psr7_request_json_encode(RequestInterface $request): string |
|
66
|
|
|
{ |
|
67
|
5 |
|
return json_try_encode(psr7_request_encode($request)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function psr7_request_encode(RequestInterface $request): array |
|
71
|
|
|
{ |
|
72
|
10 |
|
$json = []; |
|
73
|
10 |
|
$json['protocol_version'] = $request->getProtocolVersion(); |
|
74
|
10 |
|
$json['method'] = $request->getMethod(); |
|
75
|
10 |
|
$json['uri'] = (string)$request->getUri(); |
|
76
|
10 |
|
$json['headers'] = sort_headers($request->getHeaders()); |
|
77
|
10 |
|
$json['body'] = \base64_encode((string)$request->getBody()); |
|
78
|
|
|
|
|
79
|
10 |
|
return $json; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @throws NotAnEncodedRequestException |
|
84
|
|
|
*/ |
|
85
|
|
|
function psr7_request_json_decode(string $json): RequestInterface |
|
86
|
|
|
{ |
|
87
|
2 |
|
return psr7_request_decode(json_try_decode($json, true)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @throws NotAnEncodedRequestException |
|
92
|
|
|
*/ |
|
93
|
|
|
function psr7_request_decode(array $json): RequestInterface |
|
94
|
|
|
{ |
|
95
|
|
|
$properties = [ |
|
96
|
4 |
|
'protocol_version', |
|
97
|
|
|
'method', |
|
98
|
|
|
'uri', |
|
99
|
|
|
'headers', |
|
100
|
|
|
'body', |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
4 |
|
validate_array($json, $properties, NotAnEncodedRequestException::class); |
|
104
|
|
|
|
|
105
|
2 |
|
return new Request( |
|
106
|
2 |
|
$json['method'], |
|
107
|
2 |
|
$json['uri'], |
|
108
|
2 |
|
$json['headers'], |
|
109
|
2 |
|
\base64_decode($json['body'], true), |
|
110
|
2 |
|
$json['protocol_version'] |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
function psr7_uploaded_file_json_encode(UploadedFileInterface $uploadedFile): string |
|
115
|
|
|
{ |
|
116
|
5 |
|
return json_try_encode(psr7_uploaded_file_encode($uploadedFile)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
function psr7_uploaded_file_encode(UploadedFileInterface $uploadedFile): array |
|
120
|
|
|
{ |
|
121
|
260 |
|
$json = []; |
|
122
|
260 |
|
$json['filename'] = $uploadedFile->getClientFilename(); |
|
123
|
260 |
|
$json['media_type'] = $uploadedFile->getClientMediaType(); |
|
124
|
260 |
|
$json['error'] = $uploadedFile->getError(); |
|
125
|
260 |
|
$json['size'] = $uploadedFile->getSize(); |
|
126
|
260 |
|
$json['stream'] = \base64_encode((string)$uploadedFile->getStream()); |
|
127
|
|
|
|
|
128
|
260 |
|
return $json; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @throws NotAnEncodedUploadedFileException |
|
133
|
|
|
*/ |
|
134
|
|
|
function psr7_uploaded_file_json_decode(string $json): UploadedFileInterface |
|
135
|
|
|
{ |
|
136
|
2 |
|
return psr7_uploaded_file_decode(json_try_decode($json, true)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @throws NotAnEncodedUploadedFileException |
|
141
|
|
|
*/ |
|
142
|
|
|
function psr7_uploaded_file_decode(array $json): UploadedFileInterface |
|
143
|
|
|
{ |
|
144
|
|
|
$properties = [ |
|
145
|
6 |
|
'stream', |
|
146
|
|
|
'size', |
|
147
|
|
|
'error', |
|
148
|
|
|
'filename', |
|
149
|
|
|
'media_type', |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
6 |
|
validate_array($json, $properties, NotAnEncodedUploadedFileException::class); |
|
153
|
|
|
|
|
154
|
4 |
|
return new UploadedFile( |
|
155
|
4 |
|
stream_for(\base64_decode($json['stream'], true)), |
|
156
|
4 |
|
$json['size'], |
|
157
|
4 |
|
$json['error'], |
|
158
|
4 |
|
$json['filename'], |
|
159
|
4 |
|
$json['media_type'] |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
function psr7_server_request_json_encode(ServerRequestInterface $request): string |
|
164
|
|
|
{ |
|
165
|
125 |
|
return json_try_encode(psr7_server_request_encode($request)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
function psr7_server_request_encode(ServerRequestInterface $request): array |
|
169
|
|
|
{ |
|
170
|
250 |
|
$json = []; |
|
171
|
250 |
|
$json['protocol_version'] = $request->getProtocolVersion(); |
|
172
|
250 |
|
$json['method'] = $request->getMethod(); |
|
173
|
250 |
|
$json['uri'] = (string)$request->getUri(); |
|
174
|
250 |
|
$json['query_params'] = $request->getQueryParams(); |
|
175
|
250 |
|
$json['cookie_params'] = $request->getCookieParams(); |
|
176
|
250 |
|
$json['server_params'] = $request->getServerParams(); |
|
177
|
250 |
|
$json['headers'] = sort_headers($request->getHeaders()); |
|
178
|
250 |
|
$json['attributes'] = $request->getAttributes(); |
|
179
|
250 |
|
$json['body'] = \base64_encode((string)$request->getBody()); |
|
180
|
250 |
|
$json['parsed_body'] = $request->getParsedBody(); |
|
181
|
250 |
|
$json['files'] = $request->getUploadedFiles(); |
|
182
|
250 |
|
$json['files'] = Hash::flatten($json['files']); |
|
183
|
250 |
|
foreach ($json['files'] as $key => $file) { |
|
184
|
250 |
|
$json['files'][$key] = psr7_uploaded_file_encode($file); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
250 |
|
return $json; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @throws NotAnEncodedServerRequestException |
|
192
|
|
|
* @throws NotAnEncodedUploadedFileException |
|
193
|
|
|
*/ |
|
194
|
|
|
function psr7_server_request_json_decode(string $json): ServerRequestInterface |
|
195
|
|
|
{ |
|
196
|
2 |
|
return psr7_server_request_decode(json_try_decode($json, true)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @throws NotAnEncodedServerRequestException |
|
201
|
|
|
* @throws NotAnEncodedUploadedFileException |
|
202
|
|
|
*/ |
|
203
|
|
|
function psr7_server_request_decode(array $json): ServerRequestInterface |
|
204
|
|
|
{ |
|
205
|
|
|
$properties = [ |
|
206
|
4 |
|
'protocol_version', |
|
207
|
|
|
'method', |
|
208
|
|
|
'uri', |
|
209
|
|
|
'query_params', |
|
210
|
|
|
'cookie_params', |
|
211
|
|
|
'server_params', |
|
212
|
|
|
'headers', |
|
213
|
|
|
'attributes', |
|
214
|
|
|
'body', |
|
215
|
|
|
'parsed_body', |
|
216
|
|
|
'files', |
|
217
|
|
|
]; |
|
218
|
|
|
|
|
219
|
4 |
|
validate_array($json, $properties, NotAnEncodedServerRequestException::class); |
|
220
|
|
|
|
|
221
|
2 |
|
$request = (new ServerRequest( |
|
222
|
2 |
|
$json['method'], |
|
223
|
2 |
|
$json['uri'], |
|
224
|
2 |
|
$json['headers'], |
|
225
|
2 |
|
\base64_decode($json['body'], true), |
|
226
|
2 |
|
$json['protocol_version'], |
|
227
|
2 |
|
$json['server_params'] |
|
228
|
|
|
))-> |
|
229
|
2 |
|
withParsedBody($json['parsed_body'])-> |
|
230
|
2 |
|
withUploadedFiles($json['files'])-> |
|
231
|
2 |
|
withQueryParams($json['query_params'])-> |
|
232
|
2 |
|
withCookieParams($json['cookie_params']) |
|
233
|
|
|
; |
|
234
|
|
|
|
|
235
|
2 |
|
foreach ($json['attributes'] as $key => $value) { |
|
236
|
2 |
|
$request = $request->withAttribute($key, $value); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
2 |
|
if (\count($json['files']) > 0) { |
|
240
|
2 |
|
foreach ($json['files'] as $key => $file) { |
|
241
|
2 |
|
$json['files'][$key] = psr7_uploaded_file_decode($file); |
|
242
|
|
|
} |
|
243
|
2 |
|
$json['files'] = Hash::expand($json['files']); |
|
244
|
2 |
|
$request = $request->withUploadedFiles($json['files']); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
2 |
|
return $request; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
function sort_headers(array $headers): array |
|
251
|
|
|
{ |
|
252
|
270 |
|
\ksort($headers); |
|
253
|
|
|
|
|
254
|
270 |
|
return $headers; |
|
255
|
|
|
} |
|
256
|
|
|
|