Skip to content

Commit 5b1aa4e

Browse files
[Mapbox] Check for "short_code" key before using it (#1067)
* [Mapbox] Check for "short_code" key before using it * Use isset instead of !empty and fix CS
1 parent 71ea178 commit 5b1aa4e

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/Provider/Mapbox/Mapbox.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ private function updateAddressComponent(AddressBuilder $builder, string $type, a
347347

348348
case 'country':
349349
$builder->setCountry($value['text']);
350-
$builder->setCountryCode(strtoupper($value['short_code']));
350+
if (isset($value['short_code'])) {
351+
$builder->setCountryCode(strtoupper($value['short_code']));
352+
}
351353

352354
break;
353355

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:967:"{"type":"FeatureCollection","query":["princ"],"features":[{"id":"place.4899176537126140","type":"Feature","place_type":["place"],"relevance":1,"properties":{"wikidata":"Q235"},"text_it":"Principato di Monaco","language_it":"it","place_name_it":"Principato di Monaco, Principato di Monaco","text":"Principato di Monaco","language":"it","place_name":"Principato di Monaco, Principato di Monaco","bbox":[7.408977,43.724643,7.440014,43.751895],"center":[7.41974,43.73125],"geometry":{"type":"Point","coordinates":[7.41974,43.73125]},"context":[{"id":"country.3435803554126140","wikidata":"Q235","text_it":"Principato di Monaco","language_it":"it","text":"Principato di Monaco","language":"it"}]}],"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
@@ -16,6 +16,7 @@
1616
use Geocoder\Location;
1717
use Geocoder\Model\Address;
1818
use Geocoder\Model\AddressCollection;
19+
use Geocoder\Model\Bounds;
1920
use Geocoder\Query\GeocodeQuery;
2021
use Geocoder\Query\ReverseQuery;
2122
use Geocoder\Provider\Mapbox\Mapbox;
@@ -75,6 +76,49 @@ public function testGeocodeWithQuotaExceeded()
7576
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
7677
}
7778

79+
public function testGeocodePlaceWithNoCountryShortCode()
80+
{
81+
$provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
82+
83+
$query = GeocodeQuery::create('princ'); // Principato di Monaco
84+
$query = $query->withLocale('it');
85+
$query = $query->withBounds(new Bounds(
86+
35.82809688193029,
87+
-11.36323261153737,
88+
59.05992036364424,
89+
34.33947713277206
90+
));
91+
$query = $query->withLimit(1);
92+
$query = $query->withData('location_type', [
93+
Mapbox::TYPE_PLACE,
94+
Mapbox::TYPE_LOCALITY,
95+
Mapbox::TYPE_NEIGHBORHOOD,
96+
Mapbox::TYPE_POI,
97+
Mapbox::TYPE_POI_LANDMARK,
98+
]);
99+
100+
$results = $provider->geocodeQuery($query);
101+
102+
$this->assertInstanceOf(AddressCollection::class, $results);
103+
$this->assertCount(1, $results);
104+
105+
/** @var Location $result */
106+
$result = $results->first();
107+
$this->assertInstanceOf(Address::class, $result);
108+
$this->assertEquals(43.73125, $result->getCoordinates()->getLatitude(), '', 0.001);
109+
$this->assertEquals(7.41974, $result->getCoordinates()->getLongitude(), '', 0.001);
110+
$this->assertEquals('Principato di Monaco', $result->getStreetName());
111+
$this->assertEquals('Principato di Monaco', $result->getCountry()->getName());
112+
$this->assertEquals('place.4899176537126140', $result->getId());
113+
114+
// not provided
115+
$this->assertNull($result->getCountry()->getCode());
116+
$this->assertNull($result->getPostalCode());
117+
$this->assertNull($result->getLocality());
118+
$this->assertNull($result->getTimezone());
119+
$this->assertNull($result->getStreetNumber());
120+
}
121+
78122
public function testGeocodeWithRealAddress()
79123
{
80124
if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {

0 commit comments

Comments
 (0)