Skip to content

Commit

Permalink
Merge pull request #268 from formapro-forks/maxmind-binary-fix-encoding
Browse files Browse the repository at this point in the history
[1.7][maxmind] fix provider encondig. Force utf8.
  • Loading branch information
willdurand committed Oct 18, 2013
2 parents 9b9bbc3 + 3c90e66 commit ddb35bf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
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

0 comments on commit ddb35bf

Please sign in to comment.