diff --git a/src/Geocoder/Provider/AbstractProvider.php b/src/Geocoder/Provider/AbstractProvider.php index 948703754..cecb1a4a7 100644 --- a/src/Geocoder/Provider/AbstractProvider.php +++ b/src/Geocoder/Provider/AbstractProvider.php @@ -125,4 +125,16 @@ protected function getLocalhostDefaults() 'country' => 'localhost', ); } + + /** + * @param array $results + * + * @return array + */ + protected function fixEncoding(array $results) + { + return array_map(function($value) { + return is_string($value) ? utf8_encode($value) : $value; + }, $results); + } } diff --git a/src/Geocoder/Provider/GeoipProvider.php b/src/Geocoder/Provider/GeoipProvider.php index 2c456f77c..89fece0cc 100644 --- a/src/Geocoder/Provider/GeoipProvider.php +++ b/src/Geocoder/Provider/GeoipProvider.php @@ -58,7 +58,7 @@ public function getGeocodedData($address) $timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null; $region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region']; - $results = array_merge($this->getDefaults(), array( + return $this->fixEncoding(array_merge($this->getDefaults(), array( 'latitude' => $results['latitude'], 'longitude' => $results['longitude'], 'city' => $results['city'], @@ -68,13 +68,7 @@ public function getGeocodedData($address) 'country' => $results['country_name'], 'countryCode' => $results['country_code'], 'timezone' => $timezone, - )); - - $results = array_map(function($value) { - return is_string($value) ? utf8_encode($value) : $value; - }, $results); - - return $results; + ))); } /** diff --git a/src/Geocoder/Provider/MaxMindBinaryProvider.php b/src/Geocoder/Provider/MaxMindBinaryProvider.php index 9d6d0f444..7823c459c 100644 --- a/src/Geocoder/Provider/MaxMindBinaryProvider.php +++ b/src/Geocoder/Provider/MaxMindBinaryProvider.php @@ -75,14 +75,14 @@ public function getGeocodedData($address) throw new NoResultException(sprintf('No results found for IP address %s', $address)); } - return array_merge($this->getDefaults(), array( + return $this->fixEncoding(array_merge($this->getDefaults(), array( 'countryCode' => $geoIpRecord->country_code, 'country' => $geoIpRecord->country_name, 'region' => $geoIpRecord->region, 'city' => $geoIpRecord->city, 'latitude' => $geoIpRecord->latitude, 'longitude' => $geoIpRecord->longitude, - )); + ))); } /** diff --git a/tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php b/tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php index 15016a086..7570c9000 100644 --- a/tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php +++ b/tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php @@ -85,6 +85,14 @@ public function testFindLocationByIp($ip, $expectedCity, $expectedCountry) $this->assertEquals($expectedCountry, $result['country']); } + public function testShouldReturnResultsAsUtf8Encoded() + { + $provider = new MaxMindBinaryProvider($this->binaryFile); + $result = $provider->getGeocodedData('212.51.181.237'); + + $this->assertSame('Châlette-sur-loing', $result['city']); + } + public function testGetName() { $provider = new MaxMindBinaryProvider($this->binaryFile);