Skip to content

Commit ddb35bf

Browse files
committed
Merge pull request #268 from formapro-forks/maxmind-binary-fix-encoding
[1.7][maxmind] fix provider encondig. Force utf8.
2 parents 9b9bbc3 + 3c90e66 commit ddb35bf

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/Geocoder/Provider/AbstractProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ protected function getLocalhostDefaults()
125125
'country' => 'localhost',
126126
);
127127
}
128+
129+
/**
130+
* @param array $results
131+
*
132+
* @return array
133+
*/
134+
protected function fixEncoding(array $results)
135+
{
136+
return array_map(function($value) {
137+
return is_string($value) ? utf8_encode($value) : $value;
138+
}, $results);
139+
}
128140
}

src/Geocoder/Provider/GeoipProvider.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getGeocodedData($address)
5858
$timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null;
5959
$region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region'];
6060

61-
$results = array_merge($this->getDefaults(), array(
61+
return $this->fixEncoding(array_merge($this->getDefaults(), array(
6262
'latitude' => $results['latitude'],
6363
'longitude' => $results['longitude'],
6464
'city' => $results['city'],
@@ -68,13 +68,7 @@ public function getGeocodedData($address)
6868
'country' => $results['country_name'],
6969
'countryCode' => $results['country_code'],
7070
'timezone' => $timezone,
71-
));
72-
73-
$results = array_map(function($value) {
74-
return is_string($value) ? utf8_encode($value) : $value;
75-
}, $results);
76-
77-
return $results;
71+
)));
7872
}
7973

8074
/**

src/Geocoder/Provider/MaxMindBinaryProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public function getGeocodedData($address)
7575
throw new NoResultException(sprintf('No results found for IP address %s', $address));
7676
}
7777

78-
return array_merge($this->getDefaults(), array(
78+
return $this->fixEncoding(array_merge($this->getDefaults(), array(
7979
'countryCode' => $geoIpRecord->country_code,
8080
'country' => $geoIpRecord->country_name,
8181
'region' => $geoIpRecord->region,
8282
'city' => $geoIpRecord->city,
8383
'latitude' => $geoIpRecord->latitude,
8484
'longitude' => $geoIpRecord->longitude,
85-
));
85+
)));
8686
}
8787

8888
/**

tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ public function testFindLocationByIp($ip, $expectedCity, $expectedCountry)
8585
$this->assertEquals($expectedCountry, $result['country']);
8686
}
8787

88+
public function testShouldReturnResultsAsUtf8Encoded()
89+
{
90+
$provider = new MaxMindBinaryProvider($this->binaryFile);
91+
$result = $provider->getGeocodedData('212.51.181.237');
92+
93+
$this->assertSame('Châlette-sur-loing', $result['city']);
94+
}
95+
8896
public function testGetName()
8997
{
9098
$provider = new MaxMindBinaryProvider($this->binaryFile);

0 commit comments

Comments
 (0)