Skip to content

Commit d6f45a4

Browse files
[Nominatim] Fix Undefined property when no country (#1051)
1 parent f2ba196 commit d6f45a4

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Provider/Nominatim/Nominatim.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ private function jsonResultToLocation(\stdClass $place, bool $reverse): Location
221221
$builder->setStreetName($place->address->road ?? $place->address->pedestrian ?? null);
222222
$builder->setStreetNumber($place->address->house_number ?? null);
223223
$builder->setSubLocality($place->address->suburb ?? null);
224-
$builder->setCountry($place->address->country);
225-
$builder->setCountryCode(strtoupper($place->address->country_code));
224+
$builder->setCountry($place->address->country ?? null);
225+
$builder->setCountryCode(isset($place->address->country_code) ? strtoupper($place->address->country_code) : null);
226226

227227
$builder->setCoordinates(floatval($place->lat), floatval($place->lon));
228228

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:2885:"[{"place_id":235443463,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"relation","osm_id":365331,"boundingbox":["35.2889616","47.0921462","6.6272658","18.7844746"],"lat":"42.6384261","lon":"12.674297","display_name":"Italia","place_rank":4,"category":"boundary","type":"administrative","importance":0.9931019031440554,"icon":"https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png","address":{"country":"Italia","country_code":"it"}},{"place_id":102271541,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":62194430,"boundingbox":["-62.1749853","-62.1734171","-58.5166348","-58.5136685"],"lat":"-62.17462","lon":"-58.5155525","display_name":"Italia","place_rank":17,"category":"waterway","type":"stream","importance":0.43500000000000005,"address":{"stream":"Italia"}},{"place_id":201878718,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":545359188,"boundingbox":["4.8709701","4.8849163","-75.6274157","-75.6099632"],"lat":"4.8761951","lon":"-75.6211137","display_name":"La Italia, Santa Rosa de Cabal, Risaralda, 661020, Colombia","place_rank":17,"category":"waterway","type":"stream","importance":0.42500000000000004,"address":{"stream":"La Italia","county":"Santa Rosa de Cabal","state":"Risaralda","postcode":"661020","country":"Colombia","country_code":"co"}},{"place_id":236149336,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"relation","osm_id":6425790,"boundingbox":["-27.4489953","-27.4462815","-59.0336046","-59.0305577"],"lat":"-27.447628100000003","lon":"-59.03208140978377","display_name":"Italia, Resistencia, Departamento San Fernando, Chaco, Argentina","place_rank":18,"category":"boundary","type":"administrative","importance":0.41000000000000003,"icon":"https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png","address":{"city_district":"Italia","city":"Resistencia","county":"Resistencia","state_district":"Departamento San Fernando","state":"Chaco","country":"Argentina","country_code":"ar"}},{"place_id":236333103,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"relation","osm_id":7188406,"boundingbox":["4.8053955","4.8069968","-75.7651302","-75.7633548"],"lat":"4.8062121","lon":"-75.76422986762702","display_name":"La Italia, Olímpica, Perimetro Urbano Pereira, Pereira, Risaralda, Colombia","place_rank":18,"category":"boundary","type":"administrative","importance":0.4,"icon":"https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png","address":{"city_district":"La Italia","city":"Olímpica","county":"Perimetro Urbano Pereira","state":"Risaralda","country":"Colombia","country_code":"co"}}]";

src/Provider/Nominatim/Tests/NominatimTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,23 @@ public function testGeocodeNoOSMId()
180180
$this->assertEquals(null, $result->getOSMId());
181181
$this->assertEquals(null, $result->getOSMType());
182182
}
183+
184+
public function testGeocodeNoCountry()
185+
{
186+
$provider = Nominatim::withOpenStreetMapServer($this->getHttpClient(), 'Geocoder PHP/Nominatim Provider/Nominatim Test');
187+
$results = $provider->geocodeQuery(GeocodeQuery::create('Italia'));
188+
189+
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
190+
$this->assertCount(5, $results);
191+
192+
$result = $results->get(1);
193+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
194+
$this->assertEquals('Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', $result->getAttribution());
195+
196+
$this->assertEquals('Italia', $result->getDisplayName());
197+
$this->assertEquals('waterway', $result->getCategory());
198+
$this->assertEquals('62194430', $result->getOSMId());
199+
$this->assertEquals('way', $result->getOSMType());
200+
$this->assertEquals(null, $result->getCountry());
201+
}
183202
}

0 commit comments

Comments
 (0)