1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2018 - present |
4
|
|
|
* Google Maps PHP - GoogleMapsResponse.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 5/9/2018 |
8
|
|
|
* MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\GoogleMaps\Http; |
12
|
|
|
|
13
|
|
|
use Biscolab\GoogleMaps\Exception\RequestException; |
14
|
|
|
use Biscolab\GoogleMaps\Exception\ResponseException; |
15
|
|
|
use Biscolab\GoogleMaps\Fields\GoogleMapsResponseFields; |
16
|
|
|
use Biscolab\GoogleMaps\Values\GoogleMapsResponseStatusValues; |
17
|
|
|
use GuzzleHttp\Psr7\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class GoogleMapsResponse |
21
|
|
|
* @package Biscolab\GoogleMaps\Http |
22
|
|
|
*/ |
23
|
|
|
class GoogleMapsResponse |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Response |
28
|
|
|
*/ |
29
|
|
|
protected $response = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Single result |
33
|
|
|
* When the Places service returns results from a details request, it places them within a single result |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
* @see https://developers.google.com/places/web-service/details#PlaceDetailsResults |
37
|
|
|
*/ |
38
|
|
|
protected $result = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* contains an array of places, with information about each. |
42
|
|
|
* The Places API returns up to 20 establishment results per query. |
43
|
|
|
* Additionally, political results may be returned which serve to identify the area of the request. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
* @see https://developers.google.com/places/web-service/search#PlaceSearchResults |
47
|
|
|
*/ |
48
|
|
|
protected $results = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* contains metadata on the request. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
* @see https://developers.google.com/places/web-service/search#PlaceSearchStatusCodes |
55
|
|
|
*/ |
56
|
|
|
protected $status = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $error_message = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $array_response = null; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var null|array |
70
|
|
|
*/ |
71
|
|
|
protected $http_status_code = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* may contain a set of attributions about this listing which must be displayed to the user (some listings may not have attribution). |
75
|
|
|
* |
76
|
|
|
* @var null|array |
77
|
|
|
* @since 0.5.0 |
78
|
|
|
*/ |
79
|
|
|
protected $html_attributions = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var null|string |
83
|
|
|
* @since 0.5.0 |
84
|
|
|
*/ |
85
|
|
|
protected $next_page_token = null; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* GoogleMapsResponse constructor. |
89
|
|
|
* |
90
|
|
|
* @param Response $response |
91
|
|
|
*/ |
92
|
8 |
|
public function __construct(Response $response) |
93
|
|
|
{ |
94
|
|
|
|
95
|
8 |
|
$this->setResponse($response); |
96
|
|
|
|
97
|
8 |
|
$this->parseResponse(); |
98
|
|
|
|
99
|
6 |
|
$this->checkHttpStatusCode(); |
100
|
6 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Response $response |
104
|
|
|
* |
105
|
|
|
* @return GoogleMapsResponse |
106
|
|
|
*/ |
107
|
8 |
|
public function setResponse(Response $response): GoogleMapsResponse |
108
|
|
|
{ |
109
|
|
|
|
110
|
8 |
|
$this->response = $response; |
111
|
|
|
|
112
|
8 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return GoogleMapsResponse |
117
|
|
|
* |
118
|
|
|
* @throws RequestException |
119
|
|
|
* @throws ResponseException |
120
|
|
|
*/ |
121
|
8 |
|
protected function parseResponse(): GoogleMapsResponse |
122
|
|
|
{ |
123
|
|
|
|
124
|
8 |
|
$json_response = $this->response->getBody()->getContents(); |
125
|
8 |
|
$array_response = $this->toArray($json_response); |
126
|
8 |
|
$result = null; |
127
|
8 |
|
$results = null; |
128
|
|
|
|
129
|
8 |
|
if (empty($array_response[GoogleMapsResponseFields::STATUS])) { |
130
|
|
|
throw new ResponseException('Missing "status" in GoogleMapsApi Response'); |
131
|
|
|
} |
132
|
8 |
|
$this->setStatus($array_response[GoogleMapsResponseFields::STATUS]); |
133
|
|
|
|
134
|
8 |
|
if ($this->getStatus() != GoogleMapsResponseStatusValues::OK) { |
135
|
2 |
|
$error_message = 'Something went wrong'; |
136
|
2 |
|
if (!empty($array_response[GoogleMapsResponseFields::ERROR_MESSAGE])) { |
137
|
|
|
$error_message = $array_response[GoogleMapsResponseFields::ERROR_MESSAGE]; |
138
|
2 |
|
$this->setErrorMessage($error_message); |
139
|
|
|
} elseif (!empty($array_response[GoogleMapsResponseFields::STATUS])) { |
140
|
|
|
$error_message .= ': ' . $array_response[GoogleMapsResponseFields::STATUS]; |
141
|
2 |
|
$this->setErrorMessage($error_message); |
142
|
2 |
|
} |
143
|
2 |
|
throw new RequestException($error_message); |
144
|
|
|
|
145
|
2 |
|
} |
146
|
|
|
|
147
|
|
|
if (!empty($array_response[GoogleMapsResponseFields::RESULTS])) { |
148
|
|
|
$results = $array_response[GoogleMapsResponseFields::RESULTS]; |
149
|
6 |
|
|
150
|
4 |
|
} elseif (!empty($array_response[GoogleMapsResponseFields::CANDIDATES])) { |
151
|
|
|
$results = $array_response[GoogleMapsResponseFields::CANDIDATES]; |
152
|
2 |
|
|
153
|
1 |
|
}elseif (!empty($array_response[GoogleMapsResponseFields::RESULT])) { |
154
|
|
|
$result = $array_response[GoogleMapsResponseFields::RESULT]; |
155
|
1 |
|
|
156
|
|
|
} else { |
157
|
|
|
$result = $array_response; |
158
|
|
|
} |
159
|
1 |
|
$this->setResult($result); |
160
|
|
|
$this->setResults($results); |
161
|
6 |
|
|
162
|
6 |
|
if (!empty($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS])) { |
163
|
|
|
$this->setHtmlAttributions($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS]); |
164
|
6 |
|
} |
165
|
|
|
|
166
|
|
|
if (!empty($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN])) { |
167
|
|
|
$this->setNextPageToken($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN]); |
168
|
6 |
|
} |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
6 |
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $json_response |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function toArray(string $json_response): array |
179
|
|
|
{ |
180
|
8 |
|
|
181
|
|
|
$this->array_response = json_decode($json_response, true); |
182
|
|
|
|
183
|
8 |
|
return $this->array_response; |
184
|
|
|
} |
185
|
8 |
|
|
186
|
|
|
/** |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getStatus(): string |
190
|
|
|
{ |
191
|
8 |
|
|
192
|
|
|
return $this->status; |
193
|
|
|
} |
194
|
8 |
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $status |
197
|
|
|
* |
198
|
|
|
* @return GoogleMapsResponse |
199
|
|
|
*/ |
200
|
|
|
public function setStatus(string $status) |
201
|
|
|
{ |
202
|
8 |
|
|
203
|
|
|
$this->status = $status; |
204
|
|
|
|
205
|
8 |
|
return $this; |
206
|
|
|
} |
207
|
8 |
|
|
208
|
|
|
/** |
209
|
|
|
* Check HTTP status code (silent/No exceptions!) |
210
|
|
|
* @return int |
211
|
|
|
*/ |
212
|
|
|
protected function checkHttpStatusCode(): int |
213
|
|
|
{ |
214
|
6 |
|
|
215
|
|
|
$this->http_status_code = $this->response->getStatusCode(); |
|
|
|
|
216
|
|
|
|
217
|
6 |
|
return $this->http_status_code; |
218
|
|
|
} |
219
|
6 |
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function getResults() |
224
|
|
|
{ |
225
|
5 |
|
|
226
|
|
|
return $this->results; |
227
|
|
|
} |
228
|
5 |
|
|
229
|
|
|
/** |
230
|
|
|
* @param array $results |
231
|
|
|
* |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function setResults(?array $results) |
235
|
|
|
{ |
236
|
6 |
|
|
237
|
|
|
$this->results = $results; |
238
|
|
|
|
239
|
6 |
|
return $this; |
240
|
|
|
} |
241
|
6 |
|
|
242
|
|
|
/** |
243
|
|
|
* @return array |
244
|
|
|
* @since v0.6.0 |
245
|
|
|
*/ |
246
|
|
|
public function getResult() |
247
|
|
|
{ |
248
|
1 |
|
|
249
|
|
|
return $this->result; |
250
|
|
|
} |
251
|
1 |
|
|
252
|
|
|
/** |
253
|
|
|
* @param array $result |
254
|
|
|
* |
255
|
|
|
* @return $this |
256
|
|
|
* @since v0.6.0 |
257
|
|
|
*/ |
258
|
|
|
public function setResult(?array $result) |
259
|
|
|
{ |
260
|
6 |
|
|
261
|
|
|
$this->result = $result; |
262
|
|
|
|
263
|
6 |
|
return $this; |
264
|
|
|
} |
265
|
6 |
|
|
266
|
|
|
/** |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function getArrayResponse(): array |
270
|
|
|
{ |
271
|
|
|
|
272
|
|
|
return $this->array_response; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param array $array_response |
277
|
|
|
* |
278
|
|
|
* @return GoogleMapsResponse |
279
|
|
|
*/ |
280
|
|
|
public function setArrayResponse(array $array_response): GoogleMapsResponse |
281
|
|
|
{ |
282
|
|
|
|
283
|
|
|
$this->array_response = $array_response; |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return mixed |
290
|
|
|
*/ |
291
|
|
|
public function getErrorMessage() |
292
|
|
|
{ |
293
|
|
|
|
294
|
|
|
return $this->error_message; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param $error_message |
299
|
|
|
* |
300
|
|
|
* @return GoogleMapsResponse |
301
|
|
|
*/ |
302
|
|
|
public function setErrorMessage($error_message): GoogleMapsResponse |
303
|
|
|
{ |
304
|
2 |
|
|
305
|
|
|
$this->error_message = $error_message; |
306
|
|
|
|
307
|
2 |
|
return $this; |
308
|
|
|
} |
309
|
2 |
|
|
310
|
|
|
/** |
311
|
|
|
* @return int |
312
|
|
|
*/ |
313
|
|
|
public function getHttpStatusCode(): int |
314
|
|
|
{ |
315
|
6 |
|
|
316
|
|
|
return intval($this->http_status_code); |
317
|
|
|
} |
318
|
6 |
|
|
319
|
|
|
/** |
320
|
|
|
* @return array|null |
321
|
|
|
*/ |
322
|
|
|
public function getHtmlAttributions(): ?array |
323
|
|
|
{ |
324
|
|
|
|
325
|
|
|
return $this->html_attributions; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param array|null $html_attributions |
330
|
|
|
* |
331
|
|
|
* @return GoogleMapsResponse |
332
|
|
|
*/ |
333
|
|
|
public function setHtmlAttributions(?array $html_attributions): GoogleMapsResponse |
334
|
|
|
{ |
335
|
|
|
|
336
|
|
|
$this->html_attributions = $html_attributions; |
337
|
|
|
|
338
|
|
|
return $this; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
public function getNextPageToken(): string |
345
|
|
|
{ |
346
|
|
|
|
347
|
|
|
return $this->next_page_token ?? ''; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param $next_page_token |
352
|
|
|
* |
353
|
|
|
* @return GoogleMapsResponse |
354
|
|
|
*/ |
355
|
|
|
public function setNextPageToken($next_page_token): GoogleMapsResponse |
356
|
|
|
{ |
357
|
|
|
|
358
|
|
|
$this->next_page_token = $next_page_token; |
359
|
|
|
|
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..