1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Geocoder package. |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Geocoder\Provider\GoogleMaps; |
14
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
16
|
|
|
use Geocoder\Exception\InvalidCredentials; |
17
|
|
|
use Geocoder\Exception\InvalidServerResponse; |
18
|
|
|
use Geocoder\Exception\QuotaExceeded; |
19
|
|
|
use Geocoder\Exception\UnsupportedOperation; |
20
|
|
|
use Geocoder\Model\AddressCollection; |
21
|
|
|
use Geocoder\Model\AddressBuilder; |
22
|
|
|
use Geocoder\Query\GeocodeQuery; |
23
|
|
|
use Geocoder\Query\ReverseQuery; |
24
|
|
|
use Geocoder\Http\Provider\AbstractHttpProvider; |
25
|
|
|
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress; |
26
|
|
|
use Geocoder\Provider\Provider; |
27
|
|
|
use Http\Client\HttpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author William Durand <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class GoogleMaps extends AbstractHttpProvider implements Provider |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
const GEOCODE_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
const REVERSE_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=%F,%F'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
private $region; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string|null |
51
|
|
|
*/ |
52
|
|
|
private $apiKey; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $clientId; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string|null |
61
|
|
|
*/ |
62
|
|
|
private $privateKey; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Google Maps for Business |
66
|
|
|
* https://developers.google.com/maps/documentation/business/. |
67
|
|
|
* |
68
|
|
|
* @param HttpClient $client An HTTP adapter |
69
|
|
|
* @param string $clientId Your Client ID |
70
|
|
|
* @param string $privateKey Your Private Key (optional) |
71
|
|
|
* @param string $region Region biasing (optional) |
72
|
|
|
* @param string $apiKey Google Geocoding API key (optional) |
73
|
|
|
* |
74
|
|
|
* @return GoogleMaps |
75
|
|
|
*/ |
76
|
4 |
|
public static function business(HttpClient $client, string $clientId, string $privateKey = null, string $region = null, string $apiKey = null) |
77
|
|
|
{ |
78
|
4 |
|
$provider = new self($client, $region, $apiKey); |
79
|
4 |
|
$provider->clientId = $clientId; |
80
|
4 |
|
$provider->privateKey = $privateKey; |
81
|
|
|
|
82
|
4 |
|
return $provider; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param HttpClient $client An HTTP adapter |
87
|
|
|
* @param string $region Region biasing (optional) |
88
|
|
|
* @param string $apiKey Google Geocoding API key (optional) |
89
|
|
|
*/ |
90
|
34 |
|
public function __construct(HttpClient $client, string $region = null, string $apiKey = null) |
91
|
|
|
{ |
92
|
34 |
|
parent::__construct($client); |
93
|
|
|
|
94
|
34 |
|
$this->region = $region; |
95
|
34 |
|
$this->apiKey = $apiKey; |
96
|
34 |
|
} |
97
|
|
|
|
98
|
24 |
|
public function geocodeQuery(GeocodeQuery $query): Collection |
99
|
|
|
{ |
100
|
|
|
// Google API returns invalid data if IP address given |
101
|
|
|
// This API doesn't handle IPs |
102
|
24 |
|
if (filter_var($query->getText(), FILTER_VALIDATE_IP)) { |
103
|
3 |
|
throw new UnsupportedOperation('The GoogleMaps provider does not support IP addresses, only street addresses.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
21 |
|
$url = sprintf(self::GEOCODE_ENDPOINT_URL_SSL, rawurlencode($query->getText())); |
107
|
21 |
|
if (null !== $bounds = $query->getBounds()) { |
108
|
|
|
$url .= sprintf('&bounds=%s,%s|%s,%s', $bounds->getSouth(), $bounds->getWest(), $bounds->getNorth(), $bounds->getEast()); |
109
|
|
|
} |
110
|
|
|
|
111
|
21 |
|
return $this->fetchUrl($url, $query->getLocale(), $query->getLimit(), $query->getData('region', $this->region)); |
112
|
|
|
} |
113
|
|
|
|
114
|
9 |
|
public function reverseQuery(ReverseQuery $query): Collection |
115
|
|
|
{ |
116
|
9 |
|
$coordinate = $query->getCoordinates(); |
117
|
9 |
|
$url = sprintf(self::REVERSE_ENDPOINT_URL_SSL, $coordinate->getLatitude(), $coordinate->getLongitude()); |
118
|
|
|
|
119
|
9 |
|
if (null !== $locationType = $query->getData('location_type')) { |
120
|
|
|
$url .= '&location_type='.urlencode($locationType); |
121
|
|
|
} |
122
|
|
|
|
123
|
9 |
|
if (null !== $resultType = $query->getData('result_type')) { |
124
|
|
|
$url .= '&result_type='.urlencode($resultType); |
125
|
|
|
} |
126
|
|
|
|
127
|
9 |
|
return $this->fetchUrl($url, $query->getLocale(), $query->getLimit(), $query->getData('region', $this->region)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
11 |
|
public function getName(): string |
134
|
|
|
{ |
135
|
11 |
|
return 'google_maps'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $url |
140
|
|
|
* @param string $locale |
141
|
|
|
* |
142
|
|
|
* @return string query with extra params |
143
|
|
|
*/ |
144
|
30 |
|
private function buildQuery(string $url, string $locale = null, string $region = null) |
145
|
|
|
{ |
146
|
30 |
|
if (null !== $locale) { |
147
|
4 |
|
$url = sprintf('%s&language=%s', $url, $locale); |
148
|
|
|
} |
149
|
|
|
|
150
|
30 |
|
if (null !== $region) { |
151
|
1 |
|
$url = sprintf('%s®ion=%s', $url, $region); |
152
|
|
|
} |
153
|
|
|
|
154
|
30 |
|
if (null !== $this->apiKey) { |
155
|
2 |
|
$url = sprintf('%s&key=%s', $url, $this->apiKey); |
156
|
|
|
} |
157
|
|
|
|
158
|
30 |
|
if (null !== $this->clientId) { |
159
|
4 |
|
$url = sprintf('%s&client=%s', $url, $this->clientId); |
160
|
|
|
|
161
|
4 |
|
if (null !== $this->privateKey) { |
162
|
3 |
|
$url = $this->signQuery($url); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
30 |
|
return $url; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $url |
171
|
|
|
* @param string $locale |
172
|
|
|
* @param int $limit |
173
|
|
|
* @param string $region |
174
|
|
|
* |
175
|
|
|
* @return AddressCollection |
176
|
|
|
* |
177
|
|
|
* @throws InvalidServerResponse |
178
|
|
|
* @throws InvalidCredentials |
179
|
|
|
*/ |
180
|
30 |
|
private function fetchUrl(string $url, string $locale = null, int $limit, string $region = null): AddressCollection |
181
|
|
|
{ |
182
|
30 |
|
$url = $this->buildQuery($url, $locale, $region); |
183
|
30 |
|
$content = $this->getUrlContents($url); |
184
|
|
|
|
185
|
|
|
// Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider |
186
|
17 |
|
if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) { |
187
|
2 |
|
throw new InvalidCredentials(sprintf('Invalid client ID / API Key %s', $url)); |
188
|
|
|
} |
189
|
|
|
|
190
|
15 |
|
$json = json_decode($content); |
191
|
|
|
|
192
|
|
|
// API error |
193
|
15 |
|
if (!isset($json)) { |
194
|
|
|
throw InvalidServerResponse::create($url); |
195
|
|
|
} |
196
|
|
|
|
197
|
15 |
|
if ('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_message) { |
198
|
2 |
|
throw new InvalidCredentials(sprintf('API key is invalid %s', $url)); |
199
|
|
|
} |
200
|
|
|
|
201
|
13 |
|
if ('REQUEST_DENIED' === $json->status) { |
202
|
|
|
throw new InvalidServerResponse( |
203
|
|
|
sprintf('API access denied. Request: %s - Message: %s', $url, $json->error_message) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// you are over your quota |
208
|
13 |
|
if ('OVER_QUERY_LIMIT' === $json->status) { |
209
|
1 |
|
throw new QuotaExceeded(sprintf('Daily quota exceeded %s', $url)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// no result |
213
|
12 |
|
if (!isset($json->results) || !count($json->results) || 'OK' !== $json->status) { |
214
|
2 |
|
return new AddressCollection([]); |
215
|
|
|
} |
216
|
|
|
|
217
|
10 |
|
$results = []; |
218
|
10 |
|
foreach ($json->results as $result) { |
219
|
10 |
|
$builder = new AddressBuilder($this->getName()); |
220
|
|
|
|
221
|
|
|
// update address components |
222
|
10 |
|
foreach ($result->address_components as $component) { |
223
|
10 |
|
foreach ($component->types as $type) { |
224
|
10 |
|
$this->updateAddressComponent($builder, $type, $component); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// update coordinates |
229
|
10 |
|
$coordinates = $result->geometry->location; |
230
|
10 |
|
$builder->setCoordinates($coordinates->lat, $coordinates->lng); |
231
|
|
|
|
232
|
10 |
|
if (isset($result->geometry->bounds)) { |
233
|
5 |
|
$builder->setBounds( |
234
|
5 |
|
$result->geometry->bounds->southwest->lat, |
235
|
5 |
|
$result->geometry->bounds->southwest->lng, |
236
|
5 |
|
$result->geometry->bounds->northeast->lat, |
237
|
5 |
|
$result->geometry->bounds->northeast->lng |
238
|
|
|
); |
239
|
7 |
|
} elseif (isset($result->geometry->viewport)) { |
240
|
7 |
|
$builder->setBounds( |
241
|
7 |
|
$result->geometry->viewport->southwest->lat, |
242
|
7 |
|
$result->geometry->viewport->southwest->lng, |
243
|
7 |
|
$result->geometry->viewport->northeast->lat, |
244
|
7 |
|
$result->geometry->viewport->northeast->lng |
245
|
|
|
); |
246
|
|
|
} elseif ('ROOFTOP' === $result->geometry->location_type) { |
247
|
|
|
// Fake bounds |
248
|
|
|
$builder->setBounds( |
249
|
|
|
$coordinates->lat, |
250
|
|
|
$coordinates->lng, |
251
|
|
|
$coordinates->lat, |
252
|
|
|
$coordinates->lng |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** @var GoogleAddress $address */ |
257
|
10 |
|
$address = $builder->build(GoogleAddress::class); |
258
|
10 |
|
if (isset($result->geometry->location_type)) { |
259
|
10 |
|
$address = $address->withLocationType($result->geometry->location_type); |
260
|
|
|
} |
261
|
10 |
|
if (isset($result->types)) { |
262
|
10 |
|
$address = $address->withResultType($result->types); |
263
|
|
|
} |
264
|
10 |
|
if (isset($result->formatted_address)) { |
265
|
10 |
|
$address = $address->withFormattedAddress($result->formatted_address); |
266
|
|
|
} |
267
|
10 |
|
if ($builder->hasValue('subpremise')) { |
268
|
1 |
|
$address = $address->withSubpremise($builder->getValue('subpremise')); |
269
|
|
|
} |
270
|
10 |
|
$results[] = $address; |
271
|
|
|
|
272
|
10 |
|
if (count($results) >= $limit) { |
273
|
10 |
|
break; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
10 |
|
return new AddressCollection($results); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Update current resultSet with given key/value. |
282
|
|
|
* |
283
|
|
|
* @param AddressBuilder $builder |
284
|
|
|
* @param string $type Component type |
285
|
|
|
* @param object $values The component values |
286
|
|
|
*/ |
287
|
10 |
|
private function updateAddressComponent(AddressBuilder $builder, string $type, $values) |
288
|
|
|
{ |
289
|
|
|
switch ($type) { |
290
|
10 |
|
case 'postal_code': |
291
|
9 |
|
$builder->setPostalCode($values->long_name); |
292
|
9 |
|
break; |
293
|
|
|
|
294
|
10 |
|
case 'locality': |
295
|
10 |
|
case 'postal_town': |
296
|
10 |
|
$builder->setLocality($values->long_name); |
297
|
10 |
|
break; |
298
|
|
|
|
299
|
10 |
|
case 'administrative_area_level_1': |
300
|
10 |
|
case 'administrative_area_level_2': |
301
|
10 |
|
case 'administrative_area_level_3': |
302
|
10 |
|
case 'administrative_area_level_4': |
303
|
10 |
|
case 'administrative_area_level_5': |
304
|
10 |
|
$builder->addAdminLevel(intval(substr($type, -1)), $values->long_name, $values->short_name); |
305
|
10 |
|
break; |
306
|
|
|
|
307
|
10 |
|
case 'country': |
308
|
10 |
|
$builder->setCountry($values->long_name); |
309
|
10 |
|
$builder->setCountryCode($values->short_name); |
310
|
10 |
|
break; |
311
|
|
|
|
312
|
10 |
|
case 'street_number': |
313
|
6 |
|
$builder->setStreetNumber($values->long_name); |
314
|
6 |
|
break; |
315
|
|
|
|
316
|
10 |
|
case 'route': |
317
|
7 |
|
$builder->setStreetName($values->long_name); |
318
|
7 |
|
break; |
319
|
|
|
|
320
|
10 |
|
case 'sublocality': |
321
|
4 |
|
$builder->setSubLocality($values->long_name); |
322
|
4 |
|
break; |
323
|
|
|
|
324
|
10 |
|
case 'subpremise': |
325
|
1 |
|
$builder->setValue('subpremise', $values->long_name); |
326
|
1 |
|
break; |
327
|
|
|
|
328
|
|
|
default: |
329
|
|
|
} |
330
|
10 |
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Sign a URL with a given crypto key |
334
|
|
|
* Note that this URL must be properly URL-encoded |
335
|
|
|
* src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source. |
336
|
|
|
* |
337
|
|
|
* @param string $query Query to be signed |
338
|
|
|
* |
339
|
|
|
* @return string $query query with signature appended |
340
|
|
|
*/ |
341
|
3 |
|
private function signQuery(string $query): string |
342
|
|
|
{ |
343
|
3 |
|
$url = parse_url($query); |
344
|
|
|
|
345
|
3 |
|
$urlPartToSign = $url['path'].'?'.$url['query']; |
346
|
|
|
|
347
|
|
|
// Decode the private key into its binary format |
348
|
3 |
|
$decodedKey = base64_decode(str_replace(['-', '_'], ['+', '/'], $this->privateKey)); |
349
|
|
|
|
350
|
|
|
// Create a signature using the private key and the URL-encoded |
351
|
|
|
// string using HMAC SHA1. This signature will be binary. |
352
|
3 |
|
$signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true); |
353
|
|
|
|
354
|
3 |
|
$encodedSignature = str_replace(['+', '/'], ['-', '_'], base64_encode($signature)); |
355
|
|
|
|
356
|
3 |
|
return sprintf('%s&signature=%s', $query, $encodedSignature); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|