|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Geocoder package. |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @license MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Geocoder\Provider; |
| 12 | + |
| 13 | +use Geocoder\HttpAdapter\HttpAdapterInterface; |
| 14 | +use Geocoder\Exception\InvalidCredentialsException; |
| 15 | +use Geocoder\Exception\NoResultException; |
| 16 | +use Geocoder\Exception\UnsupportedException; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Antoine Corcy <contact@sbin.dk> |
| 20 | + */ |
| 21 | +class TomTomProvider extends AbstractProvider implements ProviderInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + const GEOCODE_ENDPOINT_URL = 'https://api.tomtom.com/lbs/geocoding/geocode?key=%s&maxResults=1&query=%s'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + const REVERSE_ENDPOINT_URL = 'https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=%s&point=%F,%F'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + private $apiKey = null; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param HttpAdapterInterface $adapter An HTTP adapter. |
| 40 | + * @param string $apiKey An API key. |
| 41 | + * @param string $locale A locale (optional). |
| 42 | + */ |
| 43 | + public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null) |
| 44 | + { |
| 45 | + parent::__construct($adapter, $locale); |
| 46 | + |
| 47 | + $this->apiKey = $apiKey; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritDoc} |
| 52 | + */ |
| 53 | + public function getGeocodedData($address) |
| 54 | + { |
| 55 | + if (null === $this->apiKey) { |
| 56 | + throw new InvalidCredentialsException('No Geocoding API Key provided'); |
| 57 | + } |
| 58 | + |
| 59 | + // This API doesn't handle IPs |
| 60 | + if (filter_var($address, FILTER_VALIDATE_IP)) { |
| 61 | + throw new UnsupportedException('The TomTomProvider does not support IP addresses.'); |
| 62 | + } |
| 63 | + |
| 64 | + $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->apiKey, rawurlencode($address)); |
| 65 | + |
| 66 | + return $this->executeQuery($query); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritDoc} |
| 71 | + */ |
| 72 | + public function getReversedData(array $coordinates) |
| 73 | + { |
| 74 | + if (null === $this->apiKey) { |
| 75 | + throw new InvalidCredentialsException('No Map API Key provided'); |
| 76 | + } |
| 77 | + |
| 78 | + $query = sprintf(self::REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]); |
| 79 | + |
| 80 | + return $this->executeQuery($query); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritDoc} |
| 85 | + */ |
| 86 | + public function getName() |
| 87 | + { |
| 88 | + return 'tomtom'; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string $query |
| 93 | + * |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + protected function executeQuery($query) |
| 97 | + { |
| 98 | + if (null !== $this->getLocale()) { |
| 99 | + // Supported 2- character values are de, en, es, fr, it, nl, pl, pt, and sv. |
| 100 | + // Equivalent 3-character values are GER, ENG, SPA, FRE, ITA, DUT, POL, POR, and SWE. |
| 101 | + $query = sprintf('%s&language=%s', $query, substr($this->getLocale(), 0, 2)); |
| 102 | + } |
| 103 | + |
| 104 | + $content = $this->getAdapter()->getContent($query); |
| 105 | + |
| 106 | + try { |
| 107 | + $xml = new \SimpleXmlElement($content); |
| 108 | + } catch (\Exception $e) { |
| 109 | + throw new NoResultException(sprintf('Could not execute query %s', $query)); |
| 110 | + } |
| 111 | + |
| 112 | + $attributes = $xml->attributes(); |
| 113 | + |
| 114 | + if (isset($attributes['count']) && 0 === (int) $attributes['count']) { |
| 115 | + throw new NoResultException(sprintf('Could not execute query %s', $query)); |
| 116 | + } |
| 117 | + |
| 118 | + if (isset($attributes['errorCode'])) { |
| 119 | + if ('403' === (string) $attributes['errorCode']) { |
| 120 | + throw new InvalidCredentialsException('Map API Key provided is not valid.'); |
| 121 | + } |
| 122 | + |
| 123 | + throw new NoResultException(sprintf('Could not execute query %s', $query)); |
| 124 | + } |
| 125 | + |
| 126 | + $result = isset($xml->geoResult) ? $xml->geoResult : $xml->reverseGeoResult; |
| 127 | + |
| 128 | + return array_merge($this->getDefaults(), array( |
| 129 | + 'latitude' => isset($result->latitude) ? (double) $result->latitude : null, |
| 130 | + 'longitude' => isset($result->longitude) ? (double) $result->longitude : null, |
| 131 | + 'streetName' => isset($result->street) ? (string) $result->street : null, |
| 132 | + 'city' => isset($result->city) ? (string) $result->city : null, |
| 133 | + 'region' => isset($result->state) ? (string) $result->state : null, |
| 134 | + 'country' => isset($result->country) ? (string) $result->country : null, |
| 135 | + 'countryCode' => isset($result->countryISO3) ? (string) $result->countryISO3 : null, |
| 136 | + )); |
| 137 | + } |
| 138 | +} |
0 commit comments