Skip to content

Commit 0fbc0e1

Browse files
committed
[GraphHopper] Add GraphHopperProviders enum for provider options of GraphHopper geocoding api
Graphhopper geocoding supports different providers. Hereby the option is given to use only available providers.
1 parent 4d66689 commit 0fbc0e1

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/Provider/GraphHopper/GraphHopper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7575
}
7676

7777
$provider = $query->getData('provider');
78-
if (is_string($provider) && '' !== $provider) {
78+
if (is_string($provider) && GraphHopperProviders::tryFrom($provider)) {
7979
$url .= sprintf('&provider=%s', urlencode($provider));
8080
}
8181

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Geocoder\Provider\GraphHopper;
6+
7+
enum GraphHopperProviders: string
8+
{
9+
case Default = 'default';
10+
case Nominatim = 'nominatim';
11+
case Gisgraphy = 'gisgraphy';
12+
case Nettoolkit = 'nettoolkit';
13+
case Opencagedata = 'opencagedata';
14+
}

src/Provider/GraphHopper/Tests/GraphHopperTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,35 @@ function (RequestInterface $request) use (&$uri) {
150150
);
151151
}
152152

153+
public function testNoProviderIsAppendedWhenProviderNotInEnum(): void
154+
{
155+
$uri = '';
156+
157+
$provider = new GraphHopper(
158+
$this->getMockedHttpClientCallback(
159+
function (RequestInterface $request) use (&$uri) {
160+
$uri = (string) $request->getUri();
161+
}
162+
),
163+
'api_key'
164+
);
165+
166+
$query = GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
167+
->withLocale('fr')
168+
->withData('provider', 'invalidProvider');
169+
170+
try {
171+
$provider->geocodeQuery($query);
172+
} catch (InvalidServerResponse $e) {
173+
}
174+
175+
$this->assertEquals('https://graphhopper.com/api/1/geocode'.
176+
'?q=242+Acklam+Road%2C+London%2C+United+Kingdom'.
177+
'&key=api_key&locale=fr&limit=5',
178+
$uri
179+
);
180+
}
181+
153182
public function testReverseWithRealCoordinates(): void
154183
{
155184
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {

0 commit comments

Comments
 (0)