Skip to content

Commit 3bb7298

Browse files
authored
LocationIQ provider update from deprecated endpoint (#1153)
* Update to the LocationIQ provider to account for endpoints that are being deprecated on 1st June 2022 (+1 squashed commits) * Update LocationIQ.php Tiny update to the LocationIQ provider update to resolve reported code style error * Added region validation for LocationIQ provider as requested. Also added a new LocationIQ test for the new Exception * Reinstate the BASE_URL constant in the LocationIQ provider
1 parent 89e5d9e commit 3bb7298

10 files changed

+48
-19
lines changed

src/Provider/LocationIQ/LocationIQ.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Geocoder\Provider\LocationIQ;
1414

1515
use Geocoder\Collection;
16+
use Geocoder\Exception\InvalidArgument;
1617
use Geocoder\Exception\InvalidServerResponse;
1718
use Geocoder\Exception\InvalidCredentials;
1819
use Geocoder\Location;
@@ -32,7 +33,20 @@ final class LocationIQ extends AbstractHttpProvider implements Provider
3233
/**
3334
* @var string
3435
*/
35-
const BASE_API_URL = 'https://locationiq.org/v1';
36+
const BASE_API_URL = 'https://{region}.locationiq.com/v1';
37+
38+
/**
39+
* @var string
40+
*/
41+
protected $baseUrl;
42+
43+
/**
44+
* @var array
45+
*/
46+
protected $regions = [
47+
'us1',
48+
'eu1',
49+
];
3650

3751
/**
3852
* @var string
@@ -43,13 +57,19 @@ final class LocationIQ extends AbstractHttpProvider implements Provider
4357
* @param HttpClient $client an HTTP adapter
4458
* @param string $apiKey an API key
4559
*/
46-
public function __construct(HttpClient $client, string $apiKey)
60+
public function __construct(HttpClient $client, string $apiKey, string $region = null)
4761
{
4862
if (empty($apiKey)) {
4963
throw new InvalidCredentials('No API key provided.');
5064
}
5165

5266
$this->apiKey = $apiKey;
67+
if (null === $region) {
68+
$region = $this->regions[0];
69+
} elseif (true !== in_array($region, $this->regions, true)) {
70+
throw new InvalidArgument(sprintf('`region` must be null or one of `%s`', implode('`, `', $this->regions)));
71+
}
72+
$this->baseUrl = str_replace('{region}', $region, self::BASE_API_URL);
5373

5474
parent::__construct($client);
5575
}
@@ -177,12 +197,12 @@ private function executeQuery(string $url, string $locale = null): string
177197

178198
private function getGeocodeEndpointUrl(): string
179199
{
180-
return self::BASE_API_URL.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey;
200+
return $this->baseUrl.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey;
181201
}
182202

183203
private function getReverseEndpointUrl(): string
184204
{
185-
return self::BASE_API_URL.'/reverse.php?format=xmlv1.1&lat=%F&lon=%F&addressdetails=1&normalizecity=1&zoom=%d&key='.$this->apiKey;
205+
return $this->baseUrl.'/reverse.php?format=xmlv1.1&lat=%F&lon=%F&addressdetails=1&normalizecity=1&zoom=%d&key='.$this->apiKey;
186206
}
187207

188208
private function getNodeValue(\DOMNodeList $element)

src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_0e11fe773b90671fe731e7dea27cabb38c79578f

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_63bda5a3d6df82c1855b489355e9ee42f4e7a28f

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_6bbb167a634305cae3f0f6af70ad8b53e3425260

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_f11caf74cc5f23e3bf3602346e88138ee10816a1

Lines changed: 0 additions & 3 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
s:2304:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<searchresults timestamp='Wed, 27 Apr 22 14:49:00 +0530' attribution='https://locationiq.com/attribution' querystring='10 Downing St, London, UK' polygon='false'>
3+
<place place_id='274489782' osm_type='relation' osm_id='1879842' boundingbox="51.5032573,51.5036483,-0.1278356,-0.1273038" lat='51.50344025' lon='-0.12770820958562096' display_name='Prime Minister’s Office, 10, Downing Street, Westminster, Covent Garden, Westminster, Greater London, England, SW1A 2AA, United Kingdom' class='tourism' type='attraction' importance='0.91870641220514' icon='https://locationiq.org/static/images/mapicons/poi_point_of_interest.p.20.png'>
4+
<attraction>Prime Minister’s Office</attraction><house_number>10</house_number><road>Downing Street</road><quarter>Westminster</quarter><suburb>Covent Garden</suburb><city>Westminster</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AA</postcode><country>United Kingdom</country><country_code>gb</country_code></place><place place_id='17135717' osm_type='node' osm_id='1931404517' boundingbox="51.5032302,51.5033302,-0.1276858,-0.1275858" lat='51.5032802' lon='-0.1276358' display_name='10, Downing Street, Westminster, Covent Garden, Westminster, Greater London, England, SW1A 2AB, United Kingdom' class='place' type='house' importance='0.421'>
5+
<house_number>10</house_number><road>Downing Street</road><quarter>Westminster</quarter><suburb>Covent Garden</suburb><city>Westminster</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AB</postcode><country>United Kingdom</country><country_code>gb</country_code></place><place place_id='337785034' osm_type='node' osm_id='9529452234' boundingbox="51.5032607,51.5033607,-0.127437,-0.127337" lat='51.5033107' lon='-0.127387' display_name='Downing Street, Westminster, Covent Garden, Westminster, Greater London, England, SW1A 2AA, United Kingdom' class='man_made' type='surveillance' importance='0.311'>
6+
<road>Downing Street</road><quarter>Westminster</quarter><suburb>Covent Garden</suburb><city>Westminster</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AA</postcode><country>United Kingdom</country><country_code>gb</country_code></place></searchresults>";
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
s:402:"<?xml version='1.0' encoding='UTF-8' ?>
2-
<searchresults timestamp='Wed, 08 Jan 20 20:55:53 +0000' attribution='© LocationIQ.com CC BY 4.0, Data © OpenStreetMap contributors, ODbL 1.0' querystring='q=jsajhgsdkfjhsfkjhaldkadjaslgldasd&amp;format=xmlv1.1&amp;addressdetails=1&amp;normalizecity=1&amp;limit=5&amp;key=d4146b4eba1a69&amp;accept-language=en'>
2+
<searchresults timestamp='Wed, 27 Apr 22 09:19:00 +0000' attribution='© LocationIQ.com CC BY 4.0, Data © OpenStreetMap contributors, ODbL 1.0' querystring='q=jsajhgsdkfjhsfkjhaldkadjaslgldasd&amp;format=xmlv1.1&amp;addressdetails=1&amp;normalizecity=1&amp;limit=5&amp;key=d4146b4eba1a69&amp;accept-language=en'>
33
<error>Unable to geocode</error></searchresults>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s:879:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<reversegeocode timestamp='Wed, 27 Apr 22 14:49:01 +0530' attribution='https://locationiq.com/attribution' querystring='format=xmlv1.1&amp;lat=38.900206&amp;lon=-77.036991&amp;addressdetails=1&amp;normalizecity=1&amp;zoom=18&amp;key=d4146b4eba1a69&amp;accept-language=en'>
3+
<result place_id="334190832434" lat="38.900514" lon="-77.036971" boundingbox="38.900514,38.900514,-77.036971,-77.036971">800, 16th Street NW, Downtown, Washington, District of Columbia, District of Columbia, 20006, USA</result><addressparts><house_number>800</house_number><road>16th Street NW</road><neighbourhood>Downtown</neighbourhood><city>Washington</city><county>District of Columbia</county><state>District of Columbia</state><postcode>20006</postcode><country>United States of America</country><country_code>us</country_code></addressparts></reversegeocode>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s:1004:"<?xml version="1.0" encoding="UTF-8" ?>
2+
<reversegeocode timestamp='Wed, 27 Apr 22 14:47:55 +0530' attribution='https://locationiq.com/attribution' querystring='format=xmlv1.1&amp;lat=48.860000&amp;lon=2.350000&amp;addressdetails=1&amp;normalizecity=1&amp;zoom=18&amp;key=d4146b4eba1a69'>
3+
<result place_id="333031508" osm_type="node" osm_id="9383736463" lat="48.8600564" lon="2.3499514" boundingbox="48.8600064,48.8601064,2.3499014,2.3500014">Bistrot Beaubourg, 25, Rue Quincampoix, Quartier Saint-Merri, 4th Arrondissement, Paris, Ile-de-France, Metropolitan France, 75004, France</result><addressparts><cafe>Bistrot Beaubourg</cafe><house_number>25</house_number><road>Rue Quincampoix</road><city_block>Quartier Saint-Merri</city_block><suburb>4th Arrondissement</suburb><city_district>Paris</city_district><city>Paris</city><state>Ile-de-France</state><region>Metropolitan France</region><postcode>75004</postcode><country>France</country><country_code>fr</country_code></addressparts></reversegeocode>";

src/Provider/LocationIQ/Tests/LocationIQTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public function testGeocodeWithAddressGetsEmptyXML()
4646
$provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku'));
4747
}
4848

49+
public function testGeocodeWithInvalidRegion()
50+
{
51+
$this->expectException(\Geocoder\Exception\InvalidArgument::class);
52+
53+
$emptyXML = <<<'XML'
54+
<?xml version="1.0" encoding="utf-8"?><searchresults_empty></searchresults_empty>
55+
XML;
56+
57+
$provider = new LocationIQ($this->getMockedHttpClient($emptyXML), $_SERVER['LOCATIONIQ_API_KEY'], 'invalid');
58+
}
59+
4960
public function testReverseWithCoordinatesGetsError()
5061
{
5162
$errorXml = <<<'XML'

0 commit comments

Comments
 (0)