1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2018 - present |
4
|
|
|
* Google Maps PHP - GoogleMapsApi.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; |
12
|
|
|
|
13
|
|
|
use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields; |
14
|
|
|
use Biscolab\GoogleMaps\Enum\GoogleMapsResponseFormat; |
15
|
|
|
use Biscolab\GoogleMaps\Exception\RequestException; |
16
|
|
|
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; |
17
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsClient; |
18
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsRequest; |
19
|
|
|
use Biscolab\GoogleMaps\Values\SensorValues; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class GoogleMapsApi |
23
|
|
|
* @package Biscolab\GoogleMaps |
24
|
|
|
*/ |
25
|
|
|
class GoogleMapsApi |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var GoogleMapsApi |
30
|
|
|
*/ |
31
|
|
|
protected static $instance = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var GoogleMapsRequest |
35
|
|
|
*/ |
36
|
|
|
protected $request = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Google Maps Geocode Service API url |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $api_url = "https://maps.googleapis.com/maps/api/"; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* your own Google Maps API key |
46
|
|
|
* @var string |
47
|
|
|
* @see https://developers.google.com/maps/documentation/javascript/get-api-key |
48
|
|
|
*/ |
49
|
|
|
private $key = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Google Maps API sensor |
53
|
|
|
* @var string - true|false |
54
|
|
|
*/ |
55
|
|
|
private $sensor = 'false'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Google Maps API service name |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $service_endpoint = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $response = null; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var GoogleMapsClient |
70
|
|
|
*/ |
71
|
|
|
private $client = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
private $type = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* GoogleMapsApi constructor. |
80
|
|
|
* |
81
|
|
|
* @param array $config |
82
|
|
|
*/ |
83
|
35 |
|
public function __construct(array $config = []) |
84
|
|
|
{ |
85
|
|
|
|
86
|
|
|
// Set "API key" |
87
|
35 |
|
$key = (empty($config[GoogleMapsApiConfigFields::KEY])) ? '' : $config[GoogleMapsApiConfigFields::KEY]; |
88
|
35 |
|
$this->setKey($key); |
89
|
|
|
|
90
|
|
|
// Set "sensor" |
91
|
35 |
|
$sensor = (empty($config[GoogleMapsApiConfigFields::SENSOR])) ? SensorValues::FALSE : $config[GoogleMapsApiConfigFields::SENSOR]; |
92
|
35 |
|
$this->setSensor($sensor); |
93
|
|
|
|
94
|
|
|
// Set the endpoint |
95
|
35 |
|
$service_endpoint = (empty($config[GoogleMapsApiConfigFields::SERVICE_ENDPOINT])) ? '' : $config[GoogleMapsApiConfigFields::SERVICE_ENDPOINT]; |
96
|
35 |
|
$this->setServiceEndpoint($service_endpoint); |
97
|
|
|
|
98
|
|
|
// Set Client |
99
|
35 |
|
$this->setClient(); |
100
|
35 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getApiUrl(): string |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
return $this->api_url; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Perform the Google Maps API call |
113
|
|
|
* |
114
|
|
|
* @param GoogleMapsRequest $request |
115
|
|
|
* |
116
|
|
|
* @return Http\GoogleMapsResponse|string |
117
|
|
|
*/ |
118
|
|
|
public function get(GoogleMapsRequest $request) |
119
|
|
|
{ |
120
|
|
|
|
121
|
|
|
$this->setRequest($request); |
122
|
|
|
|
123
|
|
|
$url = $this->getUrl(); |
124
|
|
|
|
125
|
|
|
$query = $this->getQuery(); |
126
|
|
|
|
127
|
|
|
$this->response = $this->getClient()->get($url, $query); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->response; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
* @throws RequestException |
135
|
|
|
*/ |
136
|
|
|
public function getUrl(): string |
137
|
|
|
{ |
138
|
|
|
|
139
|
|
|
$url_chunks = []; |
140
|
|
|
$service_endpoint = $this->getServiceEndpoint(); |
141
|
|
|
if (!$service_endpoint) { |
142
|
|
|
throw new RequestException('Service name missing!'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$request_endpoint = $this->request->getEndpoint(); |
146
|
|
|
array_push($url_chunks, $this->api_url . $service_endpoint); |
147
|
|
|
|
148
|
|
|
if($request_endpoint) { |
149
|
|
|
array_push($url_chunks, $request_endpoint); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
array_push($url_chunks, GoogleMapsResponseFormat::JSON); |
153
|
|
|
return implode("/", $url_chunks); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
9 |
|
public function getServiceEndpoint(): string |
160
|
|
|
{ |
161
|
|
|
|
162
|
9 |
|
return $this->service_endpoint; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $service_endpoint |
167
|
|
|
* |
168
|
|
|
* @return GoogleMapsApi |
169
|
|
|
*/ |
170
|
35 |
|
public function setServiceEndpoint(string $service_endpoint): GoogleMapsApi |
171
|
|
|
{ |
172
|
|
|
|
173
|
35 |
|
$this->service_endpoint = $service_endpoint; |
174
|
|
|
|
175
|
35 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getQuery(): string |
182
|
|
|
{ |
183
|
|
|
|
184
|
|
|
$api_query = http_build_query([ |
185
|
|
|
GoogleMapsRequestFields::KEY => $this->getKey(), |
186
|
|
|
GoogleMapsRequestFields::SENSOR => $this->getSensor(), |
187
|
|
|
]); |
188
|
|
|
|
189
|
|
|
$request_query = $this->getRequest()->getQuery(); |
190
|
|
|
|
191
|
|
|
return implode('&', [ |
192
|
|
|
$api_query, |
193
|
|
|
$request_query |
194
|
|
|
]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
9 |
|
public function getKey(): string |
201
|
|
|
{ |
202
|
|
|
|
203
|
9 |
|
return $this->key; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $key |
208
|
|
|
* |
209
|
|
|
* @return GoogleMapsApi |
210
|
|
|
*/ |
211
|
35 |
|
public function setKey(string $key): GoogleMapsApi |
212
|
|
|
{ |
213
|
|
|
|
214
|
35 |
|
$this->key = $key; |
215
|
|
|
|
216
|
35 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
4 |
|
public function getSensor(): string |
223
|
|
|
{ |
224
|
|
|
|
225
|
4 |
|
return $this->sensor ? 'true' : 'false'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set sensor parameter |
230
|
|
|
* |
231
|
|
|
* @param bool|string $sensor |
232
|
|
|
* |
233
|
|
|
* @return GoogleMapsApi |
234
|
|
|
*/ |
235
|
35 |
|
public function setSensor($sensor): GoogleMapsApi |
236
|
|
|
{ |
237
|
|
|
|
238
|
35 |
|
if ($sensor !== SensorValues::FALSE) { |
239
|
17 |
|
$sensor = SensorValues::TRUE; |
240
|
|
|
} |
241
|
35 |
|
$this->sensor = $sensor; |
242
|
|
|
|
243
|
35 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return GoogleMapsRequest |
248
|
|
|
*/ |
249
|
|
|
public function getRequest(): GoogleMapsRequest |
250
|
|
|
{ |
251
|
|
|
|
252
|
|
|
return $this->request; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param GoogleMapsRequest $request |
257
|
|
|
* |
258
|
|
|
* @return GoogleMapsApi |
259
|
|
|
*/ |
260
|
|
|
public function setRequest(GoogleMapsRequest $request) |
261
|
|
|
{ |
262
|
|
|
|
263
|
|
|
$this->request = $request; |
264
|
|
|
|
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return GoogleMapsClient |
270
|
|
|
*/ |
271
|
|
|
public function getClient() |
272
|
|
|
{ |
273
|
|
|
|
274
|
|
|
return $this->client; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param GoogleMapsClient|null $client |
279
|
|
|
* |
280
|
|
|
* @return GoogleMapsApi |
281
|
|
|
*/ |
282
|
35 |
|
public function setClient(?GoogleMapsClient $client = null): GoogleMapsApi |
283
|
|
|
{ |
284
|
|
|
|
285
|
35 |
|
$this->client = $client ?? new GoogleMapsClient(); |
286
|
|
|
|
287
|
35 |
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $type |
292
|
|
|
* |
293
|
|
|
* @return GoogleMapsApi |
294
|
|
|
*/ |
295
|
|
|
protected function setType(string $type): GoogleMapsApi |
296
|
|
|
{ |
297
|
|
|
|
298
|
|
|
$this->type = $type; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
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..