Skip to content

[1.7][maxmind] fix provider encondig. Force utf8. #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Geocoder/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 2 additions & 8 deletions src/Geocoder/Provider/GeoipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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;
)));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Geocoder/Provider/MaxMindBinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
)));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down