Skip to content

Commit 69e906b

Browse files
[Mapbox] Add support for the fuzzyMatch query parameter (#1081)
* [Mapbox] Add support for the fuzzyMatch query parameter * Add test with fuzzy match * Add cached responses
1 parent c06cca2 commit 69e906b

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/Provider/Mapbox/Mapbox.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection
195195
$urlParameters['types'] = self::DEFAULT_TYPE;
196196
}
197197

198+
if (null !== $fuzzyMatch = $query->getData('fuzzy_match')) {
199+
$urlParameters['fuzzyMatch'] = $fuzzyMatch ? 'true' : 'false';
200+
}
201+
198202
if ($urlParameters) {
199203
$url .= '?'.http_build_query($urlParameters);
200204
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:4034:"{"type":"FeatureCollection","query":["wahsington"],"features":[{"id":"address.3196918775570688","type":"Feature","place_type":["address"],"relevance":0.95,"properties":{"accuracy":"street"},"text":"Washington Street","place_name":"Washington Street, Sumas, Washington 98295, United States","center":[-122.2718746,49.0017164],"geometry":{"type":"Point","coordinates":[-122.2718746,49.0017164]},"context":[{"id":"postcode.1103001428069280","text":"98295"},{"id":"place.10997180196070370","wikidata":"Q1510124","text":"Sumas"},{"id":"region.9713796497246050","wikidata":"Q1223","short_code":"US-WA","text":"Washington"},{"id":"country.19678805456372290","wikidata":"Q30","short_code":"us","text":"United States"}]},{"id":"address.1946746068053092","type":"Feature","place_type":["address"],"relevance":0.95,"properties":{"accuracy":"street"},"text":"Washington Avenue","place_name":"Washington Avenue, Victoria, British Columbia V9A 1L5, Canada","matching_text":"Washington Ave E","matching_place_name":"Washington Ave E, Victoria, British Columbia V9A 1L5, Canada","center":[-123.380986,48.4440317],"geometry":{"type":"Point","coordinates":[-123.380986,48.4440317]},"context":[{"id":"neighborhood.11535041776736560","text":"Burnside"},{"id":"postcode.7635888694089700","text":"V9A 1L5"},{"id":"place.7877779600899450","wikidata":"Q2132","text":"Victoria"},{"id":"region.9984400673322020","wikidata":"Q1974","short_code":"CA-BC","text":"British Columbia"},{"id":"country.10278600750587150","wikidata":"Q16","short_code":"ca","text":"Canada"}]},{"id":"address.499813899464900","type":"Feature","place_type":["address"],"relevance":0.95,"properties":{"accuracy":"street"},"text":"Washington Highway 6","place_name":"Washington Highway 6, Palouse, Washington 99161, United States","center":[-117.0397671,46.9302529],"geometry":{"type":"Point","coordinates":[-117.0397671,46.9302529]},"context":[{"id":"postcode.12946567993911350","text":"99161"},{"id":"place.13856508424544360","text":"Palouse"},{"id":"region.9713796497246050","wikidata":"Q1223","short_code":"US-WA","text":"Washington"},{"id":"country.19678805456372290","wikidata":"Q30","short_code":"us","text":"United States"}]},{"id":"address.8713060929842858","type":"Feature","place_type":["address"],"relevance":0.95,"properties":{"accuracy":"street"},"text":"Down River Road","place_name":"Down River Road, Lewiston, Washington 83501, United States","matching_text":"Washington Highway 128","matching_place_name":"Washington Highway 128, Lewiston, Washington 83501, United States","center":[-117.0397913,46.4312588],"geometry":{"type":"Point","coordinates":[-117.0397913,46.4312588]},"context":[{"id":"neighborhood.278688","text":"Transfer"},{"id":"postcode.11154056676467040","text":"83501"},{"id":"place.9371029782746440","wikidata":"Q505539","text":"Lewiston"},{"id":"region.9713796497246050","wikidata":"Q1223","short_code":"US-WA","text":"Washington"},{"id":"country.19678805456372290","wikidata":"Q30","short_code":"us","text":"United States"}]},{"id":"address.8337368900196888","type":"Feature","place_type":["address"],"relevance":0.95,"properties":{"accuracy":"street"},"text":"South Washington Street","place_name":"South Washington Street, Moscow, Idaho 83843, United States","matching_text":"Washington","matching_place_name":"Washington, Moscow, Idaho 83843, United States","center":[-116.9997431,46.7299485],"geometry":{"type":"Point","coordinates":[-116.9997431,46.7299485]},"context":[{"id":"postcode.10023543810932580","text":"83843"},{"id":"place.19031168784618380","wikidata":"Q499927","text":"Moscow"},{"id":"region.9862000087605220","wikidata":"Q1221","short_code":"US-ID","text":"Idaho"},{"id":"country.19678805456372290","wikidata":"Q30","short_code":"us","text":"United States"}]}],"attribution":"NOTICE: © 2020 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:339:"{"type":"FeatureCollection","query":["wahsington"],"features":[],"attribution":"NOTICE: © 2020 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}";

src/Provider/Mapbox/Tests/MapboxTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,48 @@ public function testGeocodeWithRealValidApiKey()
227227
$this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName());
228228
$this->assertEquals('NY', $result->getAdminLevels()->get(2)->getCode());
229229
}
230+
231+
public function testGeocodeWithFuzzyMatch()
232+
{
233+
if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
234+
$this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
235+
}
236+
237+
$provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
238+
239+
$query = GeocodeQuery::create('wahsington'); // Washington
240+
$query = $query->withData('fuzzy_match', true);
241+
$query = $query->withBounds(new Bounds(
242+
45.54372254,
243+
-124.83609163,
244+
49.00243912,
245+
-116.91742984
246+
));
247+
248+
$results = $provider->geocodeQuery($query);
249+
$this->assertInstanceOf(AddressCollection::class, $results);
250+
$this->assertCount(5, $results);
251+
}
252+
253+
public function testGeocodeWithoutFuzzyMatch()
254+
{
255+
if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
256+
$this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
257+
}
258+
259+
$provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
260+
261+
$query = GeocodeQuery::create('wahsington'); // Washington
262+
$query = $query->withData('fuzzy_match', false);
263+
$query = $query->withBounds(new Bounds(
264+
45.54372254,
265+
-124.83609163,
266+
49.00243912,
267+
-116.91742984
268+
));
269+
270+
$results = $provider->geocodeQuery($query);
271+
$this->assertInstanceOf(AddressCollection::class, $results);
272+
$this->assertEmpty($results);
273+
}
230274
}

0 commit comments

Comments
 (0)