Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9366ff1

Browse files
committed
Fix Gpx dumper for adminLevels
1 parent 6ae5a6d commit 9366ff1

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/Geocoder/Dumper/Gpx.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,22 @@ public function dump(Address $address)
6969
*/
7070
protected function formatName(Address $address)
7171
{
72-
$name = '';
72+
$name = [];
7373
$array = $address->toArray();
74-
$attrs = array('streetNumber', 'streetName', 'postalCode', 'locality', 'county', 'region', 'country');
74+
$attrs = [
75+
['streetNumber'],
76+
['streetName'],
77+
['postalCode'],
78+
['locality'],
79+
['adminLevels', 2, 'name'],
80+
['adminLevels', 1, 'name'],
81+
['country'],
82+
];
7583

7684
foreach ($attrs as $attr) {
77-
if (isset($array[$attr]) && !empty($array[$attr])) {
78-
$name .= sprintf('%s, ', $array[$attr]);
79-
}
85+
$name[] = \igorw\get_in($array, $attr);
8086
}
8187

82-
if (strlen($name) > 1) {
83-
$name = substr($name, 0, strlen($name) - 2);
84-
}
85-
86-
return $name;
88+
return implode(', ', array_filter($name));
8789
}
8890
}

tests/Geocoder/Tests/Dumper/GpxTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ public function testDumpWithName()
120120
'east' => 2.388911);
121121

122122
$address = $this->createAddress([
123-
'latitude' => 48.8631507,
124-
'longitude' => 2.3889114,
125-
'bounds' => $bounds,
126-
'locality' => 'Paris',
123+
'latitude' => 48.8631507,
124+
'longitude' => 2.3889114,
125+
'bounds' => $bounds,
126+
'locality' => 'Paris',
127+
'streetName' => 'Avenue Gambetta',
128+
'streetNumber' => '10',
129+
'subLocality' => '20e Arrondissement',
130+
'adminLevels' => [['level' => 1, 'name' => 'Ile-de-France']],
131+
'country' => 'France'
127132
]);
128133

129134
$expected = sprintf(<<<GPX
@@ -136,7 +141,7 @@ public function testDumpWithName()
136141
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
137142
<bounds minlat="%01.6f" minlon="%01.6f" maxlat="%01.6f" maxlon="%01.6f"/>
138143
<wpt lat="%01.7f" lon="%01.7f">
139-
<name><![CDATA[Paris]]></name>
144+
<name><![CDATA[10, Avenue Gambetta, Paris, Ile-de-France, France]]></name>
140145
<type><![CDATA[Address]]></type>
141146
</wpt>
142147
</gpx>

0 commit comments

Comments
 (0)