|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use RingCentral\Psr7\Request; |
|
8
|
|
|
use RingCentral\Psr7\Response; |
|
9
|
|
|
|
|
10
|
|
|
function psr7_response_json_encode(ResponseInterface $response): string |
|
11
|
|
|
{ |
|
12
|
1 |
|
return json_try_encode(psr7_response_encode($response)); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
function psr7_response_encode(ResponseInterface $response): array |
|
16
|
|
|
{ |
|
17
|
2 |
|
$json = []; |
|
18
|
2 |
|
$json['protocol_version'] = $response->getProtocolVersion(); |
|
19
|
2 |
|
$json['status_code'] = $response->getStatusCode(); |
|
20
|
2 |
|
$json['reason_phrase'] = $response->getReasonPhrase(); |
|
21
|
2 |
|
$json['headers'] = $response->getHeaders(); |
|
22
|
2 |
|
$json['body'] = base64_encode($response->getBody()->getContents()); |
|
23
|
|
|
|
|
24
|
2 |
|
return $json; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws NotAnEncodedResponseException |
|
29
|
|
|
*/ |
|
30
|
|
|
function psr7_response_json_decode(string $json): ResponseInterface |
|
31
|
|
|
{ |
|
32
|
1 |
|
return psr7_response_decode(json_try_decode($json, true)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws NotAnEncodedResponseException |
|
37
|
|
|
*/ |
|
38
|
|
|
function psr7_response_decode(array $json): ResponseInterface |
|
39
|
|
|
{ |
|
40
|
|
|
$properties = [ |
|
41
|
2 |
|
'protocol_version', |
|
42
|
|
|
'status_code', |
|
43
|
|
|
'reason_phrase', |
|
44
|
|
|
'headers', |
|
45
|
|
|
'body', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
2 |
|
foreach ($properties as $property) { |
|
49
|
2 |
|
if (!isset($json[$property])) { |
|
50
|
2 |
|
throw new NotAnEncodedResponseException($json); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
return new Response( |
|
55
|
2 |
|
$json['status_code'], |
|
56
|
2 |
|
$json['headers'], |
|
57
|
2 |
|
base64_decode($json['body'], true), |
|
58
|
2 |
|
$json['protocol_version'], |
|
59
|
2 |
|
$json['reason_phrase'] |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function psr7_request_json_encode(RequestInterface $request): string |
|
64
|
|
|
{ |
|
65
|
1 |
|
return json_try_encode(psr7_request_encode($request)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function psr7_request_encode(RequestInterface $request): array |
|
69
|
|
|
{ |
|
70
|
2 |
|
$json = []; |
|
71
|
2 |
|
$json['protocol_version'] = $request->getProtocolVersion(); |
|
72
|
2 |
|
$json['method'] = $request->getMethod(); |
|
73
|
2 |
|
$json['uri'] = (string)$request->getUri(); |
|
74
|
2 |
|
$json['headers'] = $request->getHeaders(); |
|
75
|
2 |
|
$json['body'] = base64_encode($request->getBody()->getContents()); |
|
76
|
|
|
|
|
77
|
2 |
|
return $json; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @throws NotAnEncodedRequestException |
|
82
|
|
|
*/ |
|
83
|
|
|
function psr7_request_json_decode(string $json): RequestInterface |
|
84
|
|
|
{ |
|
85
|
1 |
|
return psr7_request_decode(json_try_decode($json, true)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @throws NotAnEncodedRequestException |
|
90
|
|
|
*/ |
|
91
|
|
|
function psr7_request_decode(array $json): RequestInterface |
|
92
|
|
|
{ |
|
93
|
|
|
$properties = [ |
|
94
|
2 |
|
'protocol_version', |
|
95
|
|
|
'method', |
|
96
|
|
|
'uri', |
|
97
|
|
|
'headers', |
|
98
|
|
|
'body', |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
2 |
|
foreach ($properties as $property) { |
|
102
|
2 |
|
if (!isset($json[$property])) { |
|
103
|
2 |
|
throw new NotAnEncodedRequestException($json); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
return new Request( |
|
108
|
2 |
|
$json['method'], |
|
109
|
2 |
|
$json['uri'], |
|
110
|
2 |
|
$json['headers'], |
|
111
|
2 |
|
base64_decode($json['body'], true), |
|
112
|
2 |
|
$json['protocol_version'] |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|