1
|
|
|
<?php |
2
|
|
|
namespace Kunnu\Dropbox; |
3
|
|
|
|
4
|
|
|
use Kunnu\Dropbox\Exceptions\DropboxClientException; |
5
|
|
|
|
6
|
|
|
class DropboxResponse |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* The HTTP status code response |
10
|
|
|
* |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
protected $httpStatusCode; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The headers returned |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $headers; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The raw body of the response |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $body; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The decoded body of the response |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $decodedBody = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The original request that returned this response |
38
|
|
|
* |
39
|
|
|
* @var DropboxRequest |
40
|
|
|
*/ |
41
|
|
|
protected $request; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new DropboxResponse instance |
45
|
|
|
* |
46
|
|
|
* @param DropboxRequest $request |
47
|
|
|
* @param string|null $body |
48
|
|
|
* @param int|null $httpStatusCode |
49
|
|
|
* @param array $headers |
50
|
|
|
*/ |
51
|
48 |
|
public function __construct(DropboxRequest $request, $body = null, $httpStatusCode = null, array $headers = []) |
52
|
|
|
{ |
53
|
48 |
|
$this->request = $request; |
54
|
48 |
|
$this->body = $body; |
55
|
48 |
|
$this->httpStatusCode = $httpStatusCode; |
56
|
48 |
|
$this->headers = $headers; |
57
|
48 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $body |
61
|
|
|
*/ |
62
|
47 |
|
public function setBody($body) |
63
|
|
|
{ |
64
|
47 |
|
$this->body = $body; |
65
|
47 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $httpStatusCode |
69
|
|
|
*/ |
70
|
48 |
|
public function setHttpStatusCode($httpStatusCode) |
71
|
|
|
{ |
72
|
48 |
|
$this->httpStatusCode = $httpStatusCode; |
73
|
48 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $headers |
77
|
|
|
*/ |
78
|
48 |
|
public function setHeaders(array $headers) |
79
|
|
|
{ |
80
|
48 |
|
$this->headers = $headers; |
81
|
48 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the Request Request |
85
|
|
|
* |
86
|
|
|
* @return \Kunnu\Dropbox\DropboxRequest |
87
|
|
|
*/ |
88
|
44 |
|
public function getRequest() |
89
|
|
|
{ |
90
|
44 |
|
return $this->request; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the Response Body |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
46 |
|
public function getBody() |
99
|
|
|
{ |
100
|
46 |
|
return $this->body; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the Decoded Body |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
44 |
|
public function getDecodedBody() |
109
|
|
|
{ |
110
|
44 |
|
if (empty($this->decodedBody) || $this->decodedBody === null) { |
111
|
|
|
//Decode the Response Body |
112
|
44 |
|
$this->decodeBody(); |
113
|
|
|
} |
114
|
|
|
|
115
|
44 |
|
return $this->decodedBody; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get Access Token for the Request |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getAccessToken() |
124
|
|
|
{ |
125
|
|
|
return $this->getRequest()->getAccessToken(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get Request Headers |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
3 |
|
public function getHeaders() |
134
|
|
|
{ |
135
|
3 |
|
return $this->headers; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the HTTP Status Code |
140
|
|
|
* |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
|
|
public function getHttpStatusCode() |
144
|
|
|
{ |
145
|
|
|
return $this->httpStatusCode; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Decode the Body |
150
|
|
|
* |
151
|
|
|
* @throws DropboxClientException |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
44 |
|
protected function decodeBody() |
156
|
|
|
{ |
157
|
44 |
|
$body = $this->getBody(); |
158
|
|
|
|
159
|
44 |
|
if (isset($this->headers['Content-Type']) && in_array('application/json', $this->headers['Content-Type'])) { |
160
|
44 |
|
$this->decodedBody = (array) json_decode((string) $body, true); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// If the response needs to be validated |
164
|
44 |
|
if ($this->getRequest()->validateResponse()) { |
165
|
|
|
//Validate Response |
166
|
44 |
|
$this->validateResponse(); |
167
|
|
|
} |
168
|
44 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Validate Response |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
* |
175
|
|
|
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException |
176
|
|
|
*/ |
177
|
44 |
|
protected function validateResponse() |
178
|
|
|
{ |
179
|
|
|
// If JSON cannot be decoded |
180
|
44 |
|
if ($this->decodedBody === null) { |
181
|
|
|
throw new DropboxClientException("Invalid Response"); |
182
|
|
|
} |
183
|
44 |
|
} |
184
|
|
|
} |
185
|
|
|
|