|
| 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\Here; |
| 14 | + |
| 15 | +use Geocoder\Collection; |
| 16 | +use Geocoder\Exception\InvalidCredentials; |
| 17 | +use Geocoder\Exception\UnsupportedOperation; |
| 18 | +use Geocoder\Model\AddressBuilder; |
| 19 | +use Geocoder\Model\AddressCollection; |
| 20 | +use Geocoder\Query\GeocodeQuery; |
| 21 | +use Geocoder\Query\ReverseQuery; |
| 22 | +use Geocoder\Http\Provider\AbstractHttpProvider; |
| 23 | +use Geocoder\Provider\Provider; |
| 24 | +use Geocoder\Provider\Here\Model\HereAddress; |
| 25 | +use Http\Client\HttpClient; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Sébastien Barré <[email protected]> |
| 29 | + */ |
| 30 | +final class Here extends AbstractHttpProvider implements Provider |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + const GEOCODE_ENDPOINT_URL = 'https://geocoder.api.here.com/6.2/geocode.json?app_id=%s&app_code=%s&searchtext=%s&gen=8'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + const REVERSE_ENDPOINT_URL = 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json?prox=%F,%F&250&app_id=%s&app_code=%s&mode=retrieveAddresses&gen=8&maxresults=%d'; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string |
| 44 | + */ |
| 45 | + private $appId = null; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + private $appCode = null; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param HttpClient $adapter An HTTP adapter. |
| 54 | + * @param string $appId An App ID. |
| 55 | + * @param string $apoCode An App code. |
| 56 | + */ |
| 57 | + public function __construct(HttpClient $client, string $appId, string $appCode) |
| 58 | + { |
| 59 | + if (empty($appId) || empty($appCode)) { |
| 60 | + throw new InvalidCredentials('Invalid or missing api key.'); |
| 61 | + } |
| 62 | + $this->appId = $appId; |
| 63 | + $this->appCode = $appCode; |
| 64 | + |
| 65 | + parent::__construct($client); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + public function geocodeQuery(GeocodeQuery $query): Collection |
| 72 | + { |
| 73 | + // This API doesn't handle IPs |
| 74 | + if (filter_var($query->getText(), FILTER_VALIDATE_IP)) { |
| 75 | + throw new UnsupportedOperation('The Here provider does not support IP addresses, only street addresses.'); |
| 76 | + } |
| 77 | + |
| 78 | + $url = sprintf(self::GEOCODE_ENDPOINT_URL, $this->appId, $this->appCode, rawurlencode($query->getText())); |
| 79 | + |
| 80 | + if (null !== $query->getLocale()) { |
| 81 | + $url = sprintf('%s&language=%s', $url, $query->getLocale()); |
| 82 | + } |
| 83 | + |
| 84 | + return $this->executeQuery($url, $query->getLimit()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function reverseQuery(ReverseQuery $query): Collection |
| 91 | + { |
| 92 | + $coordinates = $query->getCoordinates(); |
| 93 | + $url = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates->getLatitude(), $coordinates->getLongitude(), $this->appId, $this->appCode, $query->getLimit()); |
| 94 | + |
| 95 | + return $this->executeQuery($url, $query->getLimit()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param string $url |
| 100 | + * @param string $locale |
| 101 | + * @param int $limit |
| 102 | + * |
| 103 | + * @return \Geocoder\Collection |
| 104 | + */ |
| 105 | + private function executeQuery(string $url, int $limit): Collection |
| 106 | + { |
| 107 | + $content = $this->getUrlContents($url); |
| 108 | + $json = json_decode($content, true); |
| 109 | + |
| 110 | + if (isset($json['type'])) { |
| 111 | + switch ($json['type']['subtype']) { |
| 112 | + case 'InvalidInputData': |
| 113 | + throw new InvalidArgument('Input parameter validation failed.'); |
| 114 | + case 'QuotaExceeded': |
| 115 | + throw new QuotaExceeded('Valid request but quota exceeded.'); |
| 116 | + case 'InvalidCredentials': |
| 117 | + throw new InvalidCredentials('Invalid or missing api key.'); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (!isset($json['Response']) || empty($json['Response'])) { |
| 122 | + return new AddressCollection([]); |
| 123 | + } |
| 124 | + |
| 125 | + if (!isset($json['Response']['View'][0])) { |
| 126 | + return new AddressCollection([]); |
| 127 | + } |
| 128 | + |
| 129 | + $locations = $json['Response']['View'][0]['Result']; |
| 130 | + |
| 131 | + foreach ($locations as $loc) { |
| 132 | + $location = $loc['Location']; |
| 133 | + $builder = new AddressBuilder($this->getName()); |
| 134 | + $coordinates = isset($location['NavigationPosition'][0]) ? $location['NavigationPosition'][0] : $location['DisplayPosition']; |
| 135 | + $builder->setCoordinates($coordinates['Latitude'], $coordinates['Longitude']); |
| 136 | + $bounds = $location['MapView']; |
| 137 | + |
| 138 | + $builder->setBounds($bounds['BottomRight']['Latitude'], $bounds['TopLeft']['Longitude'], $bounds['TopLeft']['Latitude'], $bounds['BottomRight']['Longitude']); |
| 139 | + $builder->setStreetNumber($location['Address']['HouseNumber'] ?? null); |
| 140 | + $builder->setStreetName($location['Address']['Street'] ?? null); |
| 141 | + $builder->setPostalCode($location['Address']['PostalCode'] ?? null); |
| 142 | + $builder->setLocality($location['Address']['City'] ?? null); |
| 143 | + $builder->setSubLocality($location['Address']['District'] ?? null); |
| 144 | + $builder->setCountry($location['Address']['AdditionalData'][0]['value'] ?? null); |
| 145 | + $builder->setCountryCode($location['Address']['Country'] ?? null); |
| 146 | + |
| 147 | + $address = $builder->build(HereAddress::class); |
| 148 | + $address = $address->withLocationId($location['LocationId']); |
| 149 | + $address = $address->withLocationType($location['LocationType']); |
| 150 | + $results[] = $address; |
| 151 | + |
| 152 | + if (count($results) >= $limit) { |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return new AddressCollection($results); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * {@inheritdoc} |
| 162 | + */ |
| 163 | + public function getName(): string |
| 164 | + { |
| 165 | + return 'Here'; |
| 166 | + } |
| 167 | +} |
0 commit comments