diff --git a/src/Provider/LocationIQ/LocationIQ.php b/src/Provider/LocationIQ/LocationIQ.php index 32a51d8ce..93ab3797a 100644 --- a/src/Provider/LocationIQ/LocationIQ.php +++ b/src/Provider/LocationIQ/LocationIQ.php @@ -13,6 +13,7 @@ namespace Geocoder\Provider\LocationIQ; use Geocoder\Collection; +use Geocoder\Exception\InvalidArgument; use Geocoder\Exception\InvalidServerResponse; use Geocoder\Exception\InvalidCredentials; use Geocoder\Location; @@ -32,7 +33,20 @@ final class LocationIQ extends AbstractHttpProvider implements Provider /** * @var string */ - const BASE_API_URL = 'https://locationiq.org/v1'; + const BASE_API_URL = 'https://{region}.locationiq.com/v1'; + + /** + * @var string + */ + protected $baseUrl; + + /** + * @var array + */ + protected $regions = [ + 'us1', + 'eu1', + ]; /** * @var string @@ -43,13 +57,19 @@ final class LocationIQ extends AbstractHttpProvider implements Provider * @param HttpClient $client an HTTP adapter * @param string $apiKey an API key */ - public function __construct(HttpClient $client, string $apiKey) + public function __construct(HttpClient $client, string $apiKey, string $region = null) { if (empty($apiKey)) { throw new InvalidCredentials('No API key provided.'); } $this->apiKey = $apiKey; + if (null === $region) { + $region = $this->regions[0]; + } elseif (true !== in_array($region, $this->regions, true)) { + throw new InvalidArgument(sprintf('`region` must be null or one of `%s`', implode('`, `', $this->regions))); + } + $this->baseUrl = str_replace('{region}', $region, self::BASE_API_URL); parent::__construct($client); } @@ -177,12 +197,12 @@ private function executeQuery(string $url, string $locale = null): string private function getGeocodeEndpointUrl(): string { - return self::BASE_API_URL.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey; + return $this->baseUrl.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey; } private function getReverseEndpointUrl(): string { - return self::BASE_API_URL.'/reverse.php?format=xmlv1.1&lat=%F&lon=%F&addressdetails=1&normalizecity=1&zoom=%d&key='.$this->apiKey; + return $this->baseUrl.'/reverse.php?format=xmlv1.1&lat=%F&lon=%F&addressdetails=1&normalizecity=1&zoom=%d&key='.$this->apiKey; } private function getNodeValue(\DOMNodeList $element) diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_0e11fe773b90671fe731e7dea27cabb38c79578f b/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_0e11fe773b90671fe731e7dea27cabb38c79578f deleted file mode 100644 index 61de7e91e..000000000 --- a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_0e11fe773b90671fe731e7dea27cabb38c79578f +++ /dev/null @@ -1,3 +0,0 @@ -s:533:" - -Atlas Buoy 0.00E 0.00NAtlas Buoy 0.00E 0.00N"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_63bda5a3d6df82c1855b489355e9ee42f4e7a28f b/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_63bda5a3d6df82c1855b489355e9ee42f4e7a28f deleted file mode 100644 index 0409f45cd..000000000 --- a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_63bda5a3d6df82c1855b489355e9ee42f4e7a28f +++ /dev/null @@ -1,5 +0,0 @@ -s:1646:" - - -Prime Minister’s Office10Downing StreetSt. James'sCovent GardenLondonGreater LondonEnglandSW1A 2AAUnited Kingdomgb -10Downing StreetSt. James'sCovent GardenLondonGreater LondonEnglandSW1United Kingdomgb"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_6bbb167a634305cae3f0f6af70ad8b53e3425260 b/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_6bbb167a634305cae3f0f6af70ad8b53e3425260 deleted file mode 100644 index b5713d62a..000000000 --- a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_6bbb167a634305cae3f0f6af70ad8b53e3425260 +++ /dev/null @@ -1,3 +0,0 @@ -s:996:" - -Bistrot Beaubourg, 25, Rue Quincampoix, Beaubourg, St-Merri, 3rd Arrondissement, Paris, Ile-de-France, Metropolitan France, 75004, FranceBistrot Beaubourg25Rue QuincampoixBeaubourgSt-Merri3rd ArrondissementParisParisIle-de-FranceFrance75004fr"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_f11caf74cc5f23e3bf3602346e88138ee10816a1 b/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_f11caf74cc5f23e3bf3602346e88138ee10816a1 deleted file mode 100644 index 0f74c55c2..000000000 --- a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_f11caf74cc5f23e3bf3602346e88138ee10816a1 +++ /dev/null @@ -1,3 +0,0 @@ -s:926:" - -800, 16th Street Northwest, Downtown, Washington, District of Columbia, District of Columbia, 20006, USA80016th Street NorthwestDowntownWashingtonDistrict of ColumbiaDistrict of Columbia20006United States of Americaus"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_324ce48356e0c96193d987d830ec61ddd286e35f b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_324ce48356e0c96193d987d830ec61ddd286e35f new file mode 100644 index 000000000..6ef4ed1a1 --- /dev/null +++ b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_324ce48356e0c96193d987d830ec61ddd286e35f @@ -0,0 +1,6 @@ +s:2304:" + + +Prime Minister’s Office10Downing StreetWestminsterCovent GardenWestminsterGreater LondonEnglandSW1A 2AAUnited Kingdomgb +10Downing StreetWestminsterCovent GardenWestminsterGreater LondonEnglandSW1A 2ABUnited Kingdomgb +Downing StreetWestminsterCovent GardenWestminsterGreater LondonEnglandSW1A 2AAUnited Kingdomgb"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_fc2e704a77ab0e0d64266157eda5a034c5b1f868 b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_35ce8935bb4de8c33d18c742c8fe6e49f4d23722 similarity index 84% rename from src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_fc2e704a77ab0e0d64266157eda5a034c5b1f868 rename to src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_35ce8935bb4de8c33d18c742c8fe6e49f4d23722 index 4c9da58c6..2b0c3ad8e 100644 --- a/src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_fc2e704a77ab0e0d64266157eda5a034c5b1f868 +++ b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_35ce8935bb4de8c33d18c742c8fe6e49f4d23722 @@ -1,3 +1,3 @@ s:402:" - + Unable to geocode"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_eee20344d62638dc47f3ea3ca2a60c444bdfc428 b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_eee20344d62638dc47f3ea3ca2a60c444bdfc428 new file mode 100644 index 000000000..aa92186fc --- /dev/null +++ b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_eee20344d62638dc47f3ea3ca2a60c444bdfc428 @@ -0,0 +1,3 @@ +s:879:" + +800, 16th Street NW, Downtown, Washington, District of Columbia, District of Columbia, 20006, USA80016th Street NWDowntownWashingtonDistrict of ColumbiaDistrict of Columbia20006United States of Americaus"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_fdceb4e61df546961d8e9cf4245f3cb3603d2dc9 b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_fdceb4e61df546961d8e9cf4245f3cb3603d2dc9 new file mode 100644 index 000000000..b70be4408 --- /dev/null +++ b/src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_fdceb4e61df546961d8e9cf4245f3cb3603d2dc9 @@ -0,0 +1,3 @@ +s:1004:" + +Bistrot Beaubourg, 25, Rue Quincampoix, Quartier Saint-Merri, 4th Arrondissement, Paris, Ile-de-France, Metropolitan France, 75004, FranceBistrot Beaubourg25Rue QuincampoixQuartier Saint-Merri4th ArrondissementParisParisIle-de-FranceMetropolitan France75004Francefr"; \ No newline at end of file diff --git a/src/Provider/LocationIQ/Tests/LocationIQTest.php b/src/Provider/LocationIQ/Tests/LocationIQTest.php index 8e6fe6bec..998656dac 100644 --- a/src/Provider/LocationIQ/Tests/LocationIQTest.php +++ b/src/Provider/LocationIQ/Tests/LocationIQTest.php @@ -46,6 +46,17 @@ public function testGeocodeWithAddressGetsEmptyXML() $provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku')); } + public function testGeocodeWithInvalidRegion() + { + $this->expectException(\Geocoder\Exception\InvalidArgument::class); + + $emptyXML = <<<'XML' + +XML; + + $provider = new LocationIQ($this->getMockedHttpClient($emptyXML), $_SERVER['LOCATIONIQ_API_KEY'], 'invalid'); + } + public function testReverseWithCoordinatesGetsError() { $errorXml = <<<'XML'