-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathHere.php
More file actions
621 lines (526 loc) · 21.9 KB
/
Here.php
File metadata and controls
621 lines (526 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\Here;
use Geocoder\Collection;
use Geocoder\Exception\InvalidArgument;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Here\Model\HereAddress;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Geocoder\Query\ReverseQuery;
use Psr\Http\Client\ClientInterface;
/**
* @author Sébastien Barré <sebastien@sheub.eu>
*/
final class Here extends AbstractHttpProvider implements Provider
{
/**
* HERE Geocoding & Search API (current, recommended).
*/
public const API_V8 = 'v8';
/**
* Legacy HERE Geocoder REST API (retired December 31, 2023).
*
* @deprecated The legacy HERE Geocoder REST API was retired on December 31, 2023.
* Use {@see API_V8} and {@see createUsingApiKey()} instead.
*/
public const API_V7 = 'v7';
/**
* HERE Geocoding & Search API geocode endpoint (v8).
*
* @var string
*/
public const GEOCODE_ENDPOINT_URL = 'https://geocode.search.hereapi.com/v1/geocode';
/**
* HERE Geocoding & Search API reverse geocode endpoint (v8).
*
* @var string
*/
public const REVERSE_ENDPOINT_URL = 'https://revgeocode.search.hereapi.com/v1/revgeocode';
/**
* @deprecated Use {@see GEOCODE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const GEOCODE_ENDPOINT_URL_API_KEY = 'https://geocoder.ls.hereapi.com/6.2/geocode.json';
/**
* @deprecated Use {@see GEOCODE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const GEOCODE_ENDPOINT_URL_APP_CODE = 'https://geocoder.api.here.com/6.2/geocode.json';
/**
* @deprecated Use {@see GEOCODE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const GEOCODE_CIT_ENDPOINT_API_KEY = 'https:/geocoder.sit.ls.hereapi.com/6.2/geocode.json';
/**
* @deprecated Use {@see GEOCODE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const GEOCODE_CIT_ENDPOINT_APP_CODE = 'https://geocoder.cit.api.here.com/6.2/geocode.json';
/**
* @deprecated Use {@see REVERSE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const REVERSE_ENDPOINT_URL_API_KEY = 'https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json';
/**
* @deprecated Use {@see REVERSE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const REVERSE_ENDPOINT_URL_APP_CODE = 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json';
/**
* @deprecated Use {@see REVERSE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const REVERSE_CIT_ENDPOINT_URL_API_KEY = 'https://reverse.geocoder.sit.ls.hereapi.com/6.2/reversegeocode.json';
/**
* @deprecated Use {@see REVERSE_ENDPOINT_URL} with the v8 API instead.
*
* @var string
*/
public const REVERSE_CIT_ENDPOINT_URL_APP_CODE = 'https://reverse.geocoder.cit.api.here.com/6.2/reversegeocode.json';
/**
* Additional data parameters supported by the v7 (legacy) geocode endpoint.
*
* @deprecated These parameters are only available in the legacy HERE Geocoder API (v7),
* which was retired on December 31, 2023. Migrate to the v8 API.
*
* @var string[]
*/
public const GEOCODE_ADDITIONAL_DATA_PARAMS = [
'CrossingStreets',
'PreserveUnitDesignators',
'Country2',
'IncludeChildPOIs',
'IncludeRoutingInformation',
'AdditionalAddressProvider',
'HouseNumberMode',
'FlexibleAdminValues',
'IntersectionSnapTolerance',
'AddressRangeSqueezeOffset',
'AddressRangeSqueezeFactor',
'AddressRangeSqueezeOffset',
'IncludeShapeLevel',
'RestrictLevel',
'SuppressStreetType',
'NormalizeNames',
'IncludeMicroPointAddresses',
];
/**
* @var string
*/
private $appId;
/**
* @var string
*/
private $appCode;
/**
* @var bool
*/
private $useCIT;
/**
* @var string
*/
private $apiKey;
/**
* @var string
*/
private $apiVersion;
/**
* Creates a v7 (legacy) provider instance. For new integrations use {@see createUsingApiKey()}.
*
* @deprecated The legacy HERE Geocoder REST API was retired on December 31, 2023.
* Use {@see createUsingApiKey()} with the v8 Geocoding & Search API instead.
*
* @param ClientInterface $client an HTTP adapter
* @param string $appId a HERE App ID (v7 only)
* @param string $appCode a HERE App Code (v7 only)
* @param bool $useCIT use Customer Integration Testing environment (v7 only)
*/
public function __construct(ClientInterface $client, ?string $appId = null, ?string $appCode = null, bool $useCIT = false, string $apiVersion = self::API_V7)
{
$this->appId = $appId;
$this->appCode = $appCode;
$this->useCIT = $useCIT;
$this->apiVersion = $apiVersion;
parent::__construct($client);
}
/**
* Create a v8 (HERE Geocoding & Search API) provider using an API Key.
*
* This is the recommended factory method for all new integrations.
* See https://www.here.com/docs/bundle/geocoding-and-search-api-migration-guide/page/migration-geocoder/README.html
* for migration instructions from the legacy v7 API.
*/
public static function createUsingApiKey(ClientInterface $client, string $apiKey): self
{
$instance = new self($client, null, null, false, self::API_V8);
$instance->apiKey = $apiKey;
return $instance;
}
/**
* Create a v7 (legacy HERE Geocoder API) provider using an API Key.
*
* @deprecated The legacy HERE Geocoder REST API was retired on December 31, 2023.
* Migrate to {@see createUsingApiKey()} with the v8 Geocoding & Search API.
* See https://www.here.com/docs/bundle/geocoding-and-search-api-migration-guide/page/migration-geocoder/README.html
*/
public static function createV7UsingApiKey(ClientInterface $client, string $apiKey, bool $useCIT = false): self
{
$instance = new self($client, null, null, $useCIT, self::API_V7);
$instance->apiKey = $apiKey;
return $instance;
}
public function geocodeQuery(GeocodeQuery $query): Collection
{
// This API doesn't handle IPs
if (filter_var($query->getText(), FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The Here provider does not support IP addresses, only street addresses.');
}
if (self::API_V8 === $this->apiVersion) {
return $this->geocodeQueryV8($query);
}
return $this->geocodeQueryV7($query);
}
public function reverseQuery(ReverseQuery $query): Collection
{
if (self::API_V8 === $this->apiVersion) {
return $this->reverseQueryV8($query);
}
return $this->reverseQueryV7($query);
}
private function geocodeQueryV8(GeocodeQuery $query): Collection
{
$queryParams = [
'q' => $query->getText(),
'limit' => $query->getLimit(),
'apiKey' => $this->apiKey,
];
// Pass-through for Geocoding & Search API geo filters / sorting reference point.
// See https://www.here.com/docs/bundle/geocoding-and-search-api-v7-api-reference/page/index.html#/paths/~1geocode/get
if (null !== $at = $query->getData('at')) {
$queryParams['at'] = $at;
}
if (null !== $in = $query->getData('in')) {
$queryParams['in'] = $in;
}
if (null !== $types = $query->getData('types')) {
$queryParams['types'] = $types;
}
$qq = [];
if (null !== $country = $query->getData('country')) {
$qq[] = 'country=' . $country;
}
if (null !== $state = $query->getData('state')) {
$qq[] = 'state=' . $state;
}
if (null !== $county = $query->getData('county')) {
$qq[] = 'county=' . $county;
}
if (null !== $city = $query->getData('city')) {
$qq[] = 'city=' . $city;
}
if (!empty($qq)) {
$queryParams['qq'] = implode(';', $qq);
}
if (null !== $query->getLocale()) {
$queryParams['lang'] = $query->getLocale();
}
return $this->executeQuery(sprintf('%s?%s', self::GEOCODE_ENDPOINT_URL, http_build_query($queryParams)), $query->getLimit());
}
private function geocodeQueryV7(GeocodeQuery $query): Collection
{
$queryParams = $this->withApiCredentials([
'searchtext' => $query->getText(),
'gen' => 9,
'additionaldata' => $this->getAdditionalDataParam($query),
]);
if (null !== $query->getData('country')) {
$queryParams['country'] = $query->getData('country');
}
if (null !== $query->getData('state')) {
$queryParams['state'] = $query->getData('state');
}
if (null !== $query->getData('county')) {
$queryParams['county'] = $query->getData('county');
}
if (null !== $query->getData('city')) {
$queryParams['city'] = $query->getData('city');
}
if (null !== $query->getLocale()) {
$queryParams['language'] = $query->getLocale();
}
return $this->executeQuery(sprintf('%s?%s', $this->getBaseUrl($query), http_build_query($queryParams)), $query->getLimit());
}
private function reverseQueryV8(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$queryParams = [
'at' => sprintf('%s,%s', $coordinates->getLatitude(), $coordinates->getLongitude()),
'limit' => $query->getLimit(),
'apiKey' => $this->apiKey,
];
if (null !== $query->getLocale()) {
$queryParams['lang'] = $query->getLocale();
}
return $this->executeQuery(sprintf('%s?%s', self::REVERSE_ENDPOINT_URL, http_build_query($queryParams)), $query->getLimit());
}
private function reverseQueryV7(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$queryParams = $this->withApiCredentials([
'gen' => 9,
'mode' => 'retrieveAddresses',
'prox' => sprintf('%s,%s', $coordinates->getLatitude(), $coordinates->getLongitude()),
'maxresults' => $query->getLimit(),
]);
return $this->executeQuery(sprintf('%s?%s', $this->getBaseUrl($query), http_build_query($queryParams)), $query->getLimit());
}
private function executeQuery(string $url, int $limit): Collection
{
$content = $this->getUrlContents($url);
$json = json_decode($content, true);
if (self::API_V8 === $this->apiVersion) {
// v8 error format: {"error":"Unauthorized","error_description":"..."}
if (isset($json['error']) && 'Unauthorized' === $json['error']) {
throw new InvalidCredentials('Invalid or missing api key.');
}
if (isset($json['items'])) {
return $this->parseV8Response($json['items'], $limit);
}
return new AddressCollection([]);
}
// v7 error format: {"type":{"subtype":"InvalidCredentials"}}
if (isset($json['type'])) {
switch ($json['type']['subtype']) {
case 'InvalidInputData':
throw new InvalidArgument('Input parameter validation failed.');
case 'QuotaExceeded':
throw new QuotaExceeded('Valid request but quota exceeded.');
case 'InvalidCredentials':
throw new InvalidCredentials('Invalid or missing api key.');
}
}
if (!isset($json['Response']) || empty($json['Response'])) {
return new AddressCollection([]);
}
if (!isset($json['Response']['View'][0])) {
return new AddressCollection([]);
}
return $this->parseV7Response($json['Response']['View'][0]['Result'], $limit);
}
private function parseV8Response(array $items, int $limit): Collection
{
$results = [];
foreach ($items as $item) {
$builder = new AddressBuilder($this->getName());
$position = $item['position'];
$builder->setCoordinates($position['lat'], $position['lng']);
if (isset($item['mapView'])) {
$mapView = $item['mapView'];
$builder->setBounds($mapView['south'], $mapView['west'], $mapView['north'], $mapView['east']);
}
$address = $item['address'];
$builder->setStreetNumber($address['houseNumber'] ?? null);
$builder->setStreetName($address['street'] ?? null);
$builder->setPostalCode($address['postalCode'] ?? null);
$builder->setLocality($address['city'] ?? null);
// The Geocoding & Search API may provide both `district` and `subdistrict`. Prefer `district`
// for backward compatibility, but fall back to `subdistrict` when `district` is missing.
$builder->setSubLocality($address['district'] ?? ($address['subdistrict'] ?? null));
$builder->setCountryCode($address['countryCode'] ?? null);
$builder->setCountry($address['countryName'] ?? null);
/** @var HereAddress $hereAddress */
$hereAddress = $builder->build(HereAddress::class);
$hereAddress = $hereAddress->withLocationId($item['id'] ?? null);
$hereAddress = $hereAddress->withLocationType($item['resultType'] ?? null);
$hereAddress = $hereAddress->withLocationName($item['title'] ?? null);
$additionalData = [];
if (isset($address['label'])) {
$additionalData[] = ['key' => 'Label', 'value' => $address['label']];
}
if (isset($address['countryName'])) {
$additionalData[] = ['key' => 'CountryName', 'value' => $address['countryName']];
}
if (isset($address['state'])) {
$additionalData[] = ['key' => 'StateName', 'value' => $address['state']];
}
if (isset($address['stateCode'])) {
$additionalData[] = ['key' => 'StateCode', 'value' => $address['stateCode']];
}
if (isset($address['county'])) {
$additionalData[] = ['key' => 'CountyName', 'value' => $address['county']];
}
if (isset($address['countyCode'])) {
$additionalData[] = ['key' => 'CountyCode', 'value' => $address['countyCode']];
}
if (isset($address['district'])) {
$additionalData[] = ['key' => 'District', 'value' => $address['district']];
}
if (isset($address['subdistrict'])) {
$additionalData[] = ['key' => 'Subdistrict', 'value' => $address['subdistrict']];
}
if (isset($address['streets'])) {
$additionalData[] = ['key' => 'Streets', 'value' => $address['streets']];
}
if (isset($address['block'])) {
$additionalData[] = ['key' => 'Block', 'value' => $address['block']];
}
if (isset($address['subblock'])) {
$additionalData[] = ['key' => 'Subblock', 'value' => $address['subblock']];
}
if (isset($address['building'])) {
$additionalData[] = ['key' => 'Building', 'value' => $address['building']];
}
if (isset($address['unit'])) {
$additionalData[] = ['key' => 'Unit', 'value' => $address['unit']];
}
// Item-level metadata
foreach (
[
'politicalView' => 'PoliticalView',
'houseNumberType' => 'HouseNumberType',
'addressBlockType' => 'AddressBlockType',
'localityType' => 'LocalityType',
'administrativeAreaType' => 'AdministrativeAreaType',
'distance' => 'Distance',
] as $sourceKey => $targetKey
) {
if (isset($item[$sourceKey])) {
$additionalData[] = ['key' => $targetKey, 'value' => $item[$sourceKey]];
}
}
$hereAddress = $hereAddress->withAdditionalData($additionalData);
$results[] = $hereAddress;
if (count($results) >= $limit) {
break;
}
}
return new AddressCollection($results);
}
private function parseV7Response(array $locations, int $limit): Collection
{
$results = [];
foreach ($locations as $loc) {
$location = $loc['Location'];
$builder = new AddressBuilder($this->getName());
$coordinates = isset($location['NavigationPosition'][0]) ? $location['NavigationPosition'][0] : $location['DisplayPosition'];
$builder->setCoordinates($coordinates['Latitude'], $coordinates['Longitude']);
$bounds = $location['MapView'];
$builder->setBounds($bounds['BottomRight']['Latitude'], $bounds['TopLeft']['Longitude'], $bounds['TopLeft']['Latitude'], $bounds['BottomRight']['Longitude']);
$builder->setStreetNumber($location['Address']['HouseNumber'] ?? null);
$builder->setStreetName($location['Address']['Street'] ?? null);
$builder->setPostalCode($location['Address']['PostalCode'] ?? null);
$builder->setLocality($location['Address']['City'] ?? null);
$builder->setSubLocality($location['Address']['District'] ?? null);
$builder->setCountryCode($location['Address']['Country'] ?? null);
// The name of the country can be found in the AdditionalData.
$additionalData = $location['Address']['AdditionalData'] ?? null;
if (!empty($additionalData)) {
$builder->setCountry($additionalData[array_search('CountryName', array_column($additionalData, 'key'))]['value'] ?? null);
}
// There may be a second AdditionalData. For example if "IncludeRoutingInformation" parameter is added
$extraAdditionalData = $loc['AdditionalData'] ?? [];
/** @var HereAddress $address */
$address = $builder->build(HereAddress::class);
$address = $address->withLocationId($location['LocationId'] ?? null);
$address = $address->withLocationType($location['LocationType']);
$address = $address->withAdditionalData(array_merge($additionalData ?? [], $extraAdditionalData));
$address = $address->withShape($location['Shape'] ?? null);
$results[] = $address;
if (count($results) >= $limit) {
break;
}
}
return new AddressCollection($results);
}
public function getName(): string
{
return 'Here';
}
public function getBaseUrl(Query $query): string
{
if (self::API_V8 === $this->apiVersion) {
return ($query instanceof ReverseQuery) ? self::REVERSE_ENDPOINT_URL : self::GEOCODE_ENDPOINT_URL;
}
$usingApiKey = null !== $this->apiKey;
if ($query instanceof ReverseQuery) {
if ($this->useCIT) {
return $usingApiKey ? self::REVERSE_CIT_ENDPOINT_URL_API_KEY : self::REVERSE_CIT_ENDPOINT_URL_APP_CODE;
}
return $usingApiKey ? self::REVERSE_ENDPOINT_URL_API_KEY : self::REVERSE_ENDPOINT_URL_APP_CODE;
}
if ($this->useCIT) {
return $usingApiKey ? self::GEOCODE_CIT_ENDPOINT_API_KEY : self::GEOCODE_CIT_ENDPOINT_APP_CODE;
}
return $usingApiKey ? self::GEOCODE_ENDPOINT_URL_API_KEY : self::GEOCODE_ENDPOINT_URL_APP_CODE;
}
/**
* Get serialized additional data param (v7 only).
*/
private function getAdditionalDataParam(GeocodeQuery $query): string
{
$additionalDataParams = [
'IncludeShapeLevel' => 'country',
];
foreach (self::GEOCODE_ADDITIONAL_DATA_PARAMS as $paramKey) {
if (null !== $query->getData($paramKey)) {
$additionalDataParams[$paramKey] = $query->getData($paramKey);
}
}
return $this->serializeComponents($additionalDataParams);
}
/**
* Add API credentials to query params (v7 only).
*
* @param array<string, string> $queryParams
*
* @return array<string, string>
*/
private function withApiCredentials(array $queryParams): array
{
if (
empty($this->apiKey)
&& (empty($this->appId) || empty($this->appCode))
) {
throw new InvalidCredentials('Invalid or missing api key.');
}
if (null !== $this->apiKey) {
$queryParams['apiKey'] = $this->apiKey;
} else {
$queryParams['app_id'] = $this->appId;
$queryParams['app_code'] = $this->appCode;
}
return $queryParams;
}
/**
* Serialize the component query parameter (v7 only).
*
* @param array<string, string> $components
*/
private function serializeComponents(array $components): string
{
return implode(';', array_map(function ($name, $value) {
return sprintf('%s,%s', $name, $value);
}, array_keys($components), $components));
}
}