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