|
| 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\Exception\InvalidCredentialsException; |
| 14 | +use Geocoder\HttpAdapter\HttpAdapterInterface; |
| 15 | +use Geocoder\Exception\NoResultException; |
| 16 | +use Geocoder\Exception\UnsupportedException; |
| 17 | + |
| 18 | +/** |
| 19 | + |
| 20 | + */ |
| 21 | +class OpenCageProvider extends AbstractProvider implements ProviderInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + const GEOCODE_ENDPOINT_URL = '%s://api.opencagedata.com/geocode/v1/json?key=%s&query=%s&limit=%d&pretty=1'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private $scheme = 'http'; |
| 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 bool $useSsl Whether to use an SSL connection (optional). |
| 42 | + * @param string|null $locale A locale (optional). |
| 43 | + */ |
| 44 | + public function __construct(HttpAdapterInterface $adapter, $apiKey, $useSsl = false, $locale = null) |
| 45 | + { |
| 46 | + parent::__construct($adapter, $locale); |
| 47 | + |
| 48 | + $this->apiKey = $apiKey; |
| 49 | + $this->scheme = $useSsl ? 'https' : $this->scheme; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritDoc} |
| 54 | + */ |
| 55 | + public function getGeocodedData($address) |
| 56 | + { |
| 57 | + // This API doesn't handle IPs |
| 58 | + if (filter_var($address, FILTER_VALIDATE_IP)) { |
| 59 | + throw new UnsupportedException('The OpenCageProvider does not support IP addresses.'); |
| 60 | + } |
| 61 | + |
| 62 | + if (null === $this->apiKey) { |
| 63 | + throw new InvalidCredentialsException('No API Key provided.'); |
| 64 | + } |
| 65 | + |
| 66 | + $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->scheme, $this->apiKey, urlencode($address), $this->getMaxResults() ); |
| 67 | + |
| 68 | + return $this->executeQuery($query); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritDoc} |
| 73 | + */ |
| 74 | + public function getReversedData(array $coordinates) |
| 75 | + { |
| 76 | + // latitude, longitude |
| 77 | + $address = sprintf("%f, %f", $coordinates[0], $coordinates[1]); |
| 78 | + |
| 79 | + return $this->getGeocodedData($address); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritDoc} |
| 84 | + */ |
| 85 | + public function getName() |
| 86 | + { |
| 87 | + return 'opencage'; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $query |
| 92 | + * |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + protected function executeQuery($query) |
| 96 | + { |
| 97 | + |
| 98 | + if (null !== $this->getLocale()) { |
| 99 | + $query = sprintf('%s&language=%s', $query, $this->getLocale()); |
| 100 | + } |
| 101 | + |
| 102 | + $content = $this->getAdapter()->getContent($query); |
| 103 | + |
| 104 | + if (null === $content) { |
| 105 | + throw new NoResultException(sprintf('Could not execute query: %s', $query)); |
| 106 | + } |
| 107 | + |
| 108 | + $json = json_decode($content, true); |
| 109 | + |
| 110 | + if (!isset($json['total_results']) || $json['total_results'] == 0 ) { |
| 111 | + throw new NoResultException(sprintf('Could not find results for given query: %s', $query)); |
| 112 | + } |
| 113 | + |
| 114 | + $locations = $json['results']; |
| 115 | + |
| 116 | + if (empty($locations)) { |
| 117 | + throw new NoResultException(sprintf('Could not find results for given query: %s', $query)); |
| 118 | + } |
| 119 | + |
| 120 | + $results = array(); |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + foreach ($locations as $location) { |
| 125 | + |
| 126 | + $bounds = null; |
| 127 | + if (isset($location['bounds'])) { |
| 128 | + $bounds = array( |
| 129 | + 'south' => $location['bounds']['southwest']['lat'], |
| 130 | + 'west' => $location['bounds']['southwest']['lng'], |
| 131 | + 'north' => $location['bounds']['northeast']['lat'], |
| 132 | + 'east' => $location['bounds']['northeast']['lng'], |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + $comp = $location['components']; |
| 137 | + |
| 138 | + $results[] = array_merge($this->getDefaults(), array( |
| 139 | + 'latitude' => $location['geometry']['lat'], |
| 140 | + 'longitude' => $location['geometry']['lng'], |
| 141 | + 'bounds' => $bounds ?: null, |
| 142 | + 'streetNumber' => isset($comp['house_number']) ? $comp['house_number'] : null, |
| 143 | + 'streetName' => isset($comp['road'] ) ? $comp['road'] : null, |
| 144 | + 'cityDistrict' => isset($comp['suburb'] ) ? $comp['suburb'] : null, |
| 145 | + 'city' => isset($comp['city'] ) ? $comp['city'] : null, |
| 146 | + 'zipcode' => isset($comp['postcode'] ) ? $comp['postcode'] : null, |
| 147 | + 'county' => isset($comp['county'] ) ? $comp['county'] : null, |
| 148 | + 'region' => isset($comp['state'] ) ? $comp['state'] : null, |
| 149 | + 'country' => isset($comp['country'] ) ? $comp['country'] : null, |
| 150 | + 'countryCode' => isset($comp['country_code']) ? strtoupper($comp['country_code']) : null, |
| 151 | + 'timezone' => isset($location['annotations']['timezone']['name']) ? $location['annotations']['timezone']['name'] : null, |
| 152 | + )); |
| 153 | + } |
| 154 | + |
| 155 | + return $results; |
| 156 | + } |
| 157 | +} |
0 commit comments