From 6509bda6d207d37ab3285be4e4c01ff4112ba087 Mon Sep 17 00:00:00 2001 From: Christopher Scarre Date: Wed, 27 Apr 2022 10:14:16 +0100 Subject: [PATCH 1/4] Update to the LocationIQ provider to account for endpoints that are being deprecated on 1st June 2022 (+1 squashed commits) --- src/Provider/LocationIQ/LocationIQ.php | 20 +++++++++++++++---- ...g_0e11fe773b90671fe731e7dea27cabb38c79578f | 3 --- ...g_63bda5a3d6df82c1855b489355e9ee42f4e7a28f | 5 ----- ...g_6bbb167a634305cae3f0f6af70ad8b53e3425260 | 3 --- ...g_f11caf74cc5f23e3bf3602346e88138ee10816a1 | 3 --- ...m_324ce48356e0c96193d987d830ec61ddd286e35f | 6 ++++++ ..._35ce8935bb4de8c33d18c742c8fe6e49f4d23722} | 2 +- ...m_eee20344d62638dc47f3ea3ca2a60c444bdfc428 | 3 +++ ...m_fdceb4e61df546961d8e9cf4245f3cb3603d2dc9 | 3 +++ 9 files changed, 29 insertions(+), 19 deletions(-) delete mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_0e11fe773b90671fe731e7dea27cabb38c79578f delete mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_63bda5a3d6df82c1855b489355e9ee42f4e7a28f delete mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_6bbb167a634305cae3f0f6af70ad8b53e3425260 delete mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/locationiq.org_f11caf74cc5f23e3bf3602346e88138ee10816a1 create mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_324ce48356e0c96193d987d830ec61ddd286e35f rename src/Provider/LocationIQ/Tests/.cached_responses/{locationiq.org_fc2e704a77ab0e0d64266157eda5a034c5b1f868 => us1.locationiq.com_35ce8935bb4de8c33d18c742c8fe6e49f4d23722} (84%) create mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_eee20344d62638dc47f3ea3ca2a60c444bdfc428 create mode 100644 src/Provider/LocationIQ/Tests/.cached_responses/us1.locationiq.com_fdceb4e61df546961d8e9cf4245f3cb3603d2dc9 diff --git a/src/Provider/LocationIQ/LocationIQ.php b/src/Provider/LocationIQ/LocationIQ.php index 32a51d8ce..b2238872e 100644 --- a/src/Provider/LocationIQ/LocationIQ.php +++ b/src/Provider/LocationIQ/LocationIQ.php @@ -32,7 +32,15 @@ final class LocationIQ extends AbstractHttpProvider implements Provider /** * @var string */ - const BASE_API_URL = 'https://locationiq.org/v1'; + public $baseUrl = 'https://{region}.locationiq.com/v1'; + + /** + * @var array + */ + protected $regions = [ + 'us1', + 'eu1' + ]; /** * @var string @@ -43,13 +51,17 @@ 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 (!$region || !in_array($region, $this->regions)) { + $region = $this->regions[0]; + } + $this->baseUrl = str_replace('{region}', $region, $this->baseUrl); parent::__construct($client); } @@ -177,12 +189,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 From 413a6f7e980d513655ae2cee522a33f81f1f6187 Mon Sep 17 00:00:00 2001 From: Christopher Scarre Date: Wed, 27 Apr 2022 10:30:55 +0100 Subject: [PATCH 2/4] Update LocationIQ.php Tiny update to the LocationIQ provider update to resolve reported code style error --- src/Provider/LocationIQ/LocationIQ.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Provider/LocationIQ/LocationIQ.php b/src/Provider/LocationIQ/LocationIQ.php index b2238872e..fdbe7816c 100644 --- a/src/Provider/LocationIQ/LocationIQ.php +++ b/src/Provider/LocationIQ/LocationIQ.php @@ -39,7 +39,7 @@ final class LocationIQ extends AbstractHttpProvider implements Provider */ protected $regions = [ 'us1', - 'eu1' + 'eu1', ]; /** From d3566e0e4c284a35149991c05cf27cdea4d113e9 Mon Sep 17 00:00:00 2001 From: Christopher Scarre Date: Wed, 27 Apr 2022 14:19:07 +0100 Subject: [PATCH 3/4] Added region validation for LocationIQ provider as requested. Also added a new LocationIQ test for the new Exception --- src/Provider/LocationIQ/LocationIQ.php | 5 ++++- src/Provider/LocationIQ/Tests/LocationIQTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Provider/LocationIQ/LocationIQ.php b/src/Provider/LocationIQ/LocationIQ.php index fdbe7816c..8937f4390 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; @@ -58,8 +59,10 @@ public function __construct(HttpClient $client, string $apiKey, string $region = } $this->apiKey = $apiKey; - if (!$region || !in_array($region, $this->regions)) { + 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, $this->baseUrl); 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' From 35ec6e9bbd31386201ae67893d657e3c8dde26b2 Mon Sep 17 00:00:00 2001 From: Christopher Scarre Date: Fri, 29 Apr 2022 09:09:04 +0100 Subject: [PATCH 4/4] Reinstate the BASE_URL constant in the LocationIQ provider --- src/Provider/LocationIQ/LocationIQ.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Provider/LocationIQ/LocationIQ.php b/src/Provider/LocationIQ/LocationIQ.php index 8937f4390..93ab3797a 100644 --- a/src/Provider/LocationIQ/LocationIQ.php +++ b/src/Provider/LocationIQ/LocationIQ.php @@ -33,7 +33,12 @@ final class LocationIQ extends AbstractHttpProvider implements Provider /** * @var string */ - public $baseUrl = 'https://{region}.locationiq.com/v1'; + const BASE_API_URL = 'https://{region}.locationiq.com/v1'; + + /** + * @var string + */ + protected $baseUrl; /** * @var array @@ -64,7 +69,7 @@ public function __construct(HttpClient $client, string $apiKey, string $region = } 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, $this->baseUrl); + $this->baseUrl = str_replace('{region}', $region, self::BASE_API_URL); parent::__construct($client); }