|
| 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\Ipstack; |
| 14 | + |
| 15 | +use Geocoder\Collection; |
| 16 | +use Geocoder\Exception\InvalidArgument; |
| 17 | +use Geocoder\Exception\InvalidCredentials; |
| 18 | +use Geocoder\Exception\QuotaExceeded; |
| 19 | +use Geocoder\Exception\UnsupportedOperation; |
| 20 | +use Geocoder\Model\Address; |
| 21 | +use Geocoder\Model\AddressCollection; |
| 22 | +use Geocoder\Query\GeocodeQuery; |
| 23 | +use Geocoder\Query\ReverseQuery; |
| 24 | +use Geocoder\Http\Provider\AbstractHttpProvider; |
| 25 | +use Geocoder\Provider\Provider; |
| 26 | +use Http\Client\HttpClient; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Jonas Gielen <gielenjonas@gmail.com> |
| 30 | + */ |
| 31 | +final class Ipstack extends AbstractHttpProvider implements Provider |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + const GEOCODE_ENDPOINT_URL = 'http://api.ipstack.com/%s?access_key=%s'; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + private $apiKey; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param HttpClient $client an HTTP adapter |
| 45 | + * @param string $apiKey an API key |
| 46 | + */ |
| 47 | + public function __construct(HttpClient $client, string $apiKey) |
| 48 | + { |
| 49 | + if (empty($apiKey)) { |
| 50 | + throw new InvalidCredentials('No API key provided.'); |
| 51 | + } |
| 52 | + |
| 53 | + $this->apiKey = $apiKey; |
| 54 | + parent::__construct($client); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@inheritdoc} |
| 59 | + */ |
| 60 | + public function geocodeQuery(GeocodeQuery $query): Collection |
| 61 | + { |
| 62 | + $address = $query->getText(); |
| 63 | + |
| 64 | + // This API doesn't handle IPs |
| 65 | + if (!filter_var($address, FILTER_VALIDATE_IP)) { |
| 66 | + throw new UnsupportedOperation('The Ipstack provider does not support street addresses.'); |
| 67 | + } |
| 68 | + |
| 69 | + if (in_array($address, ['127.0.0.1', '::1'])) { |
| 70 | + return new AddressCollection([$this->getLocationForLocalhost()]); |
| 71 | + } |
| 72 | + |
| 73 | + $url = sprintf(sprintf(self::GEOCODE_ENDPOINT_URL, $address, $this->apiKey)); |
| 74 | + |
| 75 | + if (null !== $query->getLocale()) { |
| 76 | + $url = sprintf('%s&language=%s', $url, $query->getLocale()); |
| 77 | + } |
| 78 | + |
| 79 | + $body = $this->getUrlContents($url); |
| 80 | + $data = json_decode($body, true); |
| 81 | + |
| 82 | + // https://ipstack.com/documentation#errors |
| 83 | + if (isset($data['error'])) { |
| 84 | + switch ($data['error']['code']) { |
| 85 | + case 301: |
| 86 | + throw new InvalidArgument( |
| 87 | + 'Invalid request (a required parameter is missing).' |
| 88 | + ); |
| 89 | + case 303: |
| 90 | + throw new InvalidArgument( |
| 91 | + 'Bulk requests are not supported on your plan. Please upgrade your subscription.' |
| 92 | + ); |
| 93 | + case 104: |
| 94 | + throw new QuotaExceeded( |
| 95 | + 'The maximum allowed amount of monthly API requests has been reached.' |
| 96 | + ); |
| 97 | + case 101: |
| 98 | + throw new InvalidCredentials( |
| 99 | + 'No API Key was specified or an invalid API Key was specified.' |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (null === $data['latitude'] |
| 105 | + && null === $data['longitude'] |
| 106 | + && null === $data['city'] |
| 107 | + && null === $data['zip'] |
| 108 | + && null === $data['country_name'] |
| 109 | + && null === $data['country_code']) { |
| 110 | + return new AddressCollection([]); |
| 111 | + } |
| 112 | + |
| 113 | + $locations[] = Address::createFromArray([ |
| 114 | + 'providedBy' => $this->getName(), |
| 115 | + 'latitude' => $data['latitude'] ?: null, |
| 116 | + 'longitude' => $data['longitude'] ?: null, |
| 117 | + 'locality' => $data['city'] ?: null, |
| 118 | + 'postalCode' => $data['zip'] ?: null, |
| 119 | + 'country' => $data['country_name'] ?: null, |
| 120 | + 'countryCode' => $data['country_code'] ?: null, |
| 121 | + ]); |
| 122 | + |
| 123 | + return new AddressCollection($locations); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * {@inheritdoc} |
| 128 | + */ |
| 129 | + public function reverseQuery(ReverseQuery $query): Collection |
| 130 | + { |
| 131 | + throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.'); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * {@inheritdoc} |
| 136 | + */ |
| 137 | + public function getName(): string |
| 138 | + { |
| 139 | + return 'ipstack'; |
| 140 | + } |
| 141 | +} |
0 commit comments