|
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 function RingCentral\Psr7\stream_for; |
|
14
|
|
|
|
|
15
|
|
|
function psr7_response_json_encode(ResponseInterface $response): string |
|
16
|
|
|
{ |
|
17
|
1 |
|
return json_try_encode(psr7_response_encode($response)); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
function psr7_response_encode(ResponseInterface $response): array |
|
21
|
|
|
{ |
|
22
|
2 |
|
$json = []; |
|
23
|
2 |
|
$json['protocol_version'] = $response->getProtocolVersion(); |
|
24
|
2 |
|
$json['status_code'] = $response->getStatusCode(); |
|
25
|
2 |
|
$json['reason_phrase'] = $response->getReasonPhrase(); |
|
26
|
2 |
|
$json['headers'] = $response->getHeaders(); |
|
27
|
2 |
|
$json['body'] = base64_encode($response->getBody()->getContents()); |
|
28
|
|
|
|
|
29
|
2 |
|
return $json; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws NotAnEncodedResponseException |
|
34
|
|
|
*/ |
|
35
|
|
|
function psr7_response_json_decode(string $json): ResponseInterface |
|
36
|
|
|
{ |
|
37
|
1 |
|
return psr7_response_decode(json_try_decode($json, true)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws NotAnEncodedResponseException |
|
42
|
|
|
*/ |
|
43
|
|
|
function psr7_response_decode(array $json): ResponseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$properties = [ |
|
46
|
2 |
|
'protocol_version', |
|
47
|
|
|
'status_code', |
|
48
|
|
|
'reason_phrase', |
|
49
|
|
|
'headers', |
|
50
|
|
|
'body', |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
2 |
|
foreach ($properties as $property) { |
|
54
|
2 |
|
if (!isset($json[$property])) { |
|
55
|
2 |
|
throw new NotAnEncodedResponseException($json); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
return new Response( |
|
60
|
2 |
|
$json['status_code'], |
|
61
|
2 |
|
$json['headers'], |
|
62
|
2 |
|
base64_decode($json['body'], true), |
|
63
|
2 |
|
$json['protocol_version'], |
|
64
|
2 |
|
$json['reason_phrase'] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function psr7_request_json_encode(RequestInterface $request): string |
|
69
|
|
|
{ |
|
70
|
1 |
|
return json_try_encode(psr7_request_encode($request)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
function psr7_request_encode(RequestInterface $request): array |
|
74
|
|
|
{ |
|
75
|
2 |
|
$json = []; |
|
76
|
2 |
|
$json['protocol_version'] = $request->getProtocolVersion(); |
|
77
|
2 |
|
$json['method'] = $request->getMethod(); |
|
78
|
2 |
|
$json['uri'] = (string)$request->getUri(); |
|
79
|
2 |
|
$json['headers'] = $request->getHeaders(); |
|
80
|
2 |
|
$json['body'] = base64_encode($request->getBody()->getContents()); |
|
81
|
|
|
|
|
82
|
2 |
|
return $json; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws NotAnEncodedRequestException |
|
87
|
|
|
*/ |
|
88
|
|
|
function psr7_request_json_decode(string $json): RequestInterface |
|
89
|
|
|
{ |
|
90
|
1 |
|
return psr7_request_decode(json_try_decode($json, true)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @throws NotAnEncodedRequestException |
|
95
|
|
|
*/ |
|
96
|
|
|
function psr7_request_decode(array $json): RequestInterface |
|
97
|
|
|
{ |
|
98
|
|
|
$properties = [ |
|
99
|
2 |
|
'protocol_version', |
|
100
|
|
|
'method', |
|
101
|
|
|
'uri', |
|
102
|
|
|
'headers', |
|
103
|
|
|
'body', |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
2 |
|
foreach ($properties as $property) { |
|
107
|
2 |
|
if (!isset($json[$property])) { |
|
108
|
2 |
|
throw new NotAnEncodedRequestException($json); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
return new Request( |
|
113
|
2 |
|
$json['method'], |
|
114
|
2 |
|
$json['uri'], |
|
115
|
2 |
|
$json['headers'], |
|
116
|
2 |
|
base64_decode($json['body'], true), |
|
117
|
2 |
|
$json['protocol_version'] |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
function psr7_uploaded_file_json_encode(UploadedFileInterface $uploadedFile): string |
|
122
|
|
|
{ |
|
123
|
1 |
|
return json_try_encode(psr7_uploaded_file_encode($uploadedFile)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
function psr7_uploaded_file_encode(UploadedFileInterface $uploadedFile): array |
|
127
|
|
|
{ |
|
128
|
4 |
|
$json = []; |
|
129
|
4 |
|
$json['filename'] = $uploadedFile->getClientFilename(); |
|
130
|
4 |
|
$json['media_type'] = $uploadedFile->getClientMediaType(); |
|
131
|
4 |
|
$json['error'] = $uploadedFile->getError(); |
|
132
|
4 |
|
$json['size'] = $uploadedFile->getSize(); |
|
133
|
4 |
|
$json['stream'] = base64_encode($uploadedFile->getStream()->getContents()); |
|
134
|
|
|
|
|
135
|
4 |
|
return $json; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @throws NotAnEncodedUploadedFileException |
|
140
|
|
|
*/ |
|
141
|
|
|
function psr7_uploaded_file_json_decode(string $json): UploadedFileInterface |
|
142
|
|
|
{ |
|
143
|
1 |
|
return psr7_uploaded_file_decode(json_try_decode($json, true)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @throws NotAnEncodedUploadedFileException |
|
148
|
|
|
*/ |
|
149
|
|
|
function psr7_uploaded_file_decode(array $json): UploadedFileInterface |
|
150
|
|
|
{ |
|
151
|
|
|
$properties = [ |
|
152
|
2 |
|
'stream', |
|
153
|
|
|
'size', |
|
154
|
|
|
'error', |
|
155
|
|
|
'filename', |
|
156
|
|
|
'media_type', |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
2 |
|
foreach ($properties as $property) { |
|
160
|
2 |
|
if (!isset($json[$property])) { |
|
161
|
2 |
|
throw new NotAnEncodedUploadedFileException($json); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
2 |
|
return new UploadedFile( |
|
166
|
2 |
|
stream_for(base64_decode($json['stream'], true)), |
|
167
|
2 |
|
$json['size'], |
|
168
|
2 |
|
$json['error'], |
|
169
|
2 |
|
$json['filename'], |
|
170
|
2 |
|
$json['media_type'] |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
function psr7_server_request_json_encode(ServerRequestInterface $request): string |
|
175
|
|
|
{ |
|
176
|
1 |
|
return json_try_encode(psr7_server_request_encode($request)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
function psr7_server_request_encode(ServerRequestInterface $request): array |
|
180
|
|
|
{ |
|
181
|
2 |
|
$json = []; |
|
182
|
2 |
|
$json['protocol_version'] = $request->getProtocolVersion(); |
|
183
|
2 |
|
$json['method'] = $request->getMethod(); |
|
184
|
2 |
|
$json['uri'] = (string)$request->getUri(); |
|
185
|
2 |
|
$json['query_params'] = $request->getQueryParams(); |
|
186
|
2 |
|
$json['cookie_params'] = $request->getCookieParams(); |
|
187
|
2 |
|
$json['server_params'] = $request->getServerParams(); |
|
188
|
2 |
|
$json['headers'] = $request->getHeaders(); |
|
189
|
2 |
|
$json['attributes'] = $request->getAttributes(); |
|
190
|
2 |
|
$json['body'] = base64_encode($request->getBody()->getContents()); |
|
191
|
2 |
|
$json['parsed_body'] = $request->getParsedBody(); |
|
192
|
2 |
|
$json['files'] = $request->getUploadedFiles(); |
|
193
|
2 |
|
$json['files'] = Hash::flatten($json['files']); |
|
194
|
2 |
|
foreach ($json['files'] as $key => $file) { |
|
195
|
2 |
|
$json['files'][$key] = psr7_uploaded_file_encode($file); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
2 |
|
return $json; |
|
199
|
|
|
} |
|
200
|
|
|
|