Skip to content

Commit 8fedd22

Browse files
committed
[GraphHopper] Add provider parameter
Graphhopper geocoding supports different providers. Hereby the option is given to use those providers.
1 parent af1105b commit 8fedd22

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Provider/GraphHopper/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 0.6.0
6+
7+
### Added
8+
9+
- Add provider support for graphhopper geocoding API
10+
511
## 0.5.0
612

713
### Removed

src/Provider/GraphHopper/GraphHopper.php

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7474
$url .= sprintf('&bbox=%f,%f,%f,%f', $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
7575
}
7676

77+
$provider = $query->getData('provider');
78+
if (is_string($provider) && '' !== $provider) {
79+
$url .= sprintf('&provider=%s', urlencode($provider));
80+
}
81+
7782
return $this->executeQuery($url);
7883
}
7984

src/Provider/GraphHopper/Tests/GraphHopperTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
namespace Geocoder\Provider\GraphHopper\Tests;
1414

15+
use Geocoder\Exception\InvalidServerResponse;
1516
use Geocoder\IntegrationTest\BaseTestCase;
1617
use Geocoder\Model\Bounds;
1718
use Geocoder\Provider\GraphHopper\GraphHopper;
1819
use Geocoder\Query\GeocodeQuery;
1920
use Geocoder\Query\ReverseQuery;
21+
use Psr\Http\Message\RequestInterface;
2022

2123
/**
2224
* @author Gary Gale <[email protected]>
@@ -119,6 +121,35 @@ public function testGeocodeOutsideBounds(): void
119121
$this->assertCount(0, $results);
120122
}
121123

124+
public function testCorrectlyAppendsProvider(): void
125+
{
126+
$uri = '';
127+
128+
$provider = new GraphHopper(
129+
$this->getMockedHttpClientCallback(
130+
function (RequestInterface $request) use (&$uri) {
131+
$uri = (string) $request->getUri();
132+
}
133+
),
134+
'api_key'
135+
);
136+
137+
$query = GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
138+
->withLocale('fr')
139+
->withData('provider', 'default');
140+
141+
try {
142+
$provider->geocodeQuery($query);
143+
} catch (InvalidServerResponse $e) {
144+
}
145+
146+
$this->assertEquals('https://graphhopper.com/api/1/geocode'.
147+
'?q=242+Acklam+Road%2C+London%2C+United+Kingdom'.
148+
'&key=api_key&locale=fr&limit=5&provider=default',
149+
$uri
150+
);
151+
}
152+
122153
public function testReverseWithRealCoordinates(): void
123154
{
124155
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {

0 commit comments

Comments
 (0)