Skip to content

Commit

Permalink
[Mapbox] Add support for the fuzzyMatch query parameter (#1081)
Browse files Browse the repository at this point in the history
* [Mapbox] Add support for the fuzzyMatch query parameter

* Add test with fuzzy match

* Add cached responses
  • Loading branch information
DieterHolvoet committed Oct 10, 2020
1 parent 61f832c commit e592a74
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Mapbox.php
Expand Up @@ -195,6 +195,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection
$urlParameters['types'] = self::DEFAULT_TYPE;
}

if (null !== $fuzzyMatch = $query->getData('fuzzy_match')) {
$urlParameters['fuzzyMatch'] = $fuzzyMatch ? 'true' : 'false';
}

if ($urlParameters) {
$url .= '?'.http_build_query($urlParameters);
}
Expand Down
@@ -0,0 +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."}";
@@ -0,0 +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."}";
44 changes: 44 additions & 0 deletions Tests/MapboxTest.php
Expand Up @@ -227,4 +227,48 @@ public function testGeocodeWithRealValidApiKey()
$this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('NY', $result->getAdminLevels()->get(2)->getCode());
}

public function testGeocodeWithFuzzyMatch()
{
if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
$this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
}

$provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);

$query = GeocodeQuery::create('wahsington'); // Washington
$query = $query->withData('fuzzy_match', true);
$query = $query->withBounds(new Bounds(
45.54372254,
-124.83609163,
49.00243912,
-116.91742984
));

$results = $provider->geocodeQuery($query);
$this->assertInstanceOf(AddressCollection::class, $results);
$this->assertCount(5, $results);
}

public function testGeocodeWithoutFuzzyMatch()
{
if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
$this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
}

$provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);

$query = GeocodeQuery::create('wahsington'); // Washington
$query = $query->withData('fuzzy_match', false);
$query = $query->withBounds(new Bounds(
45.54372254,
-124.83609163,
49.00243912,
-116.91742984
));

$results = $provider->geocodeQuery($query);
$this->assertInstanceOf(AddressCollection::class, $results);
$this->assertEmpty($results);
}
}

0 comments on commit e592a74

Please sign in to comment.