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