From 9a0f9207bee82a78deb14f3db73389bb699286bd Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 11 Nov 2018 18:57:32 +0100 Subject: [PATCH] Fixed region resolved in GeoLite2 --- composer.json | 1 + .../src/IpGeolocation/GeoLite2LocationResolver.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 86b52828..6c49c9e4 100644 --- a/composer.json +++ b/composer.json @@ -48,6 +48,7 @@ "zendframework/zend-stdlib": "^3.0" }, "require-dev": { + "devster/ubench": "^2.0", "filp/whoops": "^2.0", "infection/infection": "^0.11.0", "phpstan/phpstan": "^0.10.0", diff --git a/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php b/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php index 60023ee3..0bced338 100644 --- a/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php +++ b/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php @@ -6,8 +6,10 @@ namespace Shlinkio\Shlink\Common\IpGeolocation; use GeoIp2\Database\Reader; use GeoIp2\Exception\AddressNotFoundException; use GeoIp2\Model\City; +use GeoIp2\Record\Subdivision; use MaxMind\Db\Reader\InvalidDatabaseException; use Shlinkio\Shlink\Common\Exception\WrongIpException; +use function Functional\first; class GeoLite2LocationResolver implements IpLocationResolverInterface { @@ -40,13 +42,16 @@ class GeoLite2LocationResolver implements IpLocationResolverInterface private function mapFields(City $city): array { + /** @var Subdivision $region */ + $region = first($city->subdivisions); + return [ 'country_code' => $city->country->isoCode ?? '', 'country_name' => $city->country->name ?? '', - 'region_name' => $city->mostSpecificSubdivision->name ?? '', + 'region_name' => $region->name ?? '', 'city' => $city->city->name ?? '', - 'latitude' => (string) $city->location->latitude, // FIXME Cast to string for BC compatibility - 'longitude' => (string) $city->location->longitude, // FIXME Cast to string for BC compatibility + 'latitude' => $city->location->latitude ?? '', + 'longitude' => $city->location->longitude ?? '', 'time_zone' => $city->location->timeZone ?? '', ]; }