mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 17:44:44 +08:00
Defined stricter model to represent one geo location
This commit is contained in:
@@ -18,7 +18,7 @@ class ChainIpLocationResolver implements IpLocationResolverInterface
|
||||
/**
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
public function resolveIpLocation(string $ipAddress): Model\Location
|
||||
{
|
||||
$error = null;
|
||||
|
||||
|
||||
@@ -10,16 +10,8 @@ class EmptyIpLocationResolver implements IpLocationResolverInterface
|
||||
/**
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
public function resolveIpLocation(string $ipAddress): Model\Location
|
||||
{
|
||||
return [
|
||||
'country_code' => '',
|
||||
'country_name' => '',
|
||||
'region_name' => '',
|
||||
'city' => '',
|
||||
'latitude' => '',
|
||||
'longitude' => '',
|
||||
'time_zone' => '',
|
||||
];
|
||||
return Model\Location::emptyInstance();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class GeoLite2LocationResolver implements IpLocationResolverInterface
|
||||
/**
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
public function resolveIpLocation(string $ipAddress): Model\Location
|
||||
{
|
||||
try {
|
||||
$city = $this->geoLiteDbReader->city($ipAddress);
|
||||
@@ -36,19 +36,19 @@ class GeoLite2LocationResolver implements IpLocationResolverInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function mapFields(City $city): array
|
||||
private function mapFields(City $city): Model\Location
|
||||
{
|
||||
/** @var Subdivision $region */
|
||||
$region = first($city->subdivisions);
|
||||
|
||||
return [
|
||||
'country_code' => $city->country->isoCode ?? '',
|
||||
'country_name' => $city->country->name ?? '',
|
||||
'region_name' => $region->name ?? '',
|
||||
'city' => $city->city->name ?? '',
|
||||
'latitude' => $city->location->latitude ?? '',
|
||||
'longitude' => $city->location->longitude ?? '',
|
||||
'time_zone' => $city->location->timeZone ?? '',
|
||||
];
|
||||
return new Model\Location(
|
||||
$city->country->isoCode ?? '',
|
||||
$city->country->name ?? '',
|
||||
$region->name ?? '',
|
||||
$city->city->name ?? '',
|
||||
(float) ($city->location->latitude ?? ''),
|
||||
(float) ($city->location->longitude ?? ''),
|
||||
$city->location->timeZone ?? ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class IpApiLocationResolver implements IpLocationResolverInterface
|
||||
/**
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
public function resolveIpLocation(string $ipAddress): Model\Location
|
||||
{
|
||||
try {
|
||||
$response = $this->httpClient->get(sprintf(self::SERVICE_PATTERN, $ipAddress));
|
||||
@@ -37,16 +37,16 @@ class IpApiLocationResolver implements IpLocationResolverInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function mapFields(array $entry): array
|
||||
private function mapFields(array $entry): Model\Location
|
||||
{
|
||||
return [
|
||||
'country_code' => $entry['countryCode'] ?? '',
|
||||
'country_name' => $entry['country'] ?? '',
|
||||
'region_name' => $entry['regionName'] ?? '',
|
||||
'city' => $entry['city'] ?? '',
|
||||
'latitude' => $entry['lat'] ?? '',
|
||||
'longitude' => $entry['lon'] ?? '',
|
||||
'time_zone' => $entry['timezone'] ?? '',
|
||||
];
|
||||
return new Model\Location(
|
||||
(string) ($entry['countryCode'] ?? ''),
|
||||
(string) ($entry['country'] ?? ''),
|
||||
(string) ($entry['regionName'] ?? ''),
|
||||
(string) ($entry['city'] ?? ''),
|
||||
(float) ($entry['lat'] ?? 0.0),
|
||||
(float) ($entry['lon'] ?? 0.0),
|
||||
(string) ($entry['timezone'] ?? '')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ interface IpLocationResolverInterface
|
||||
/**
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array;
|
||||
public function resolveIpLocation(string $ipAddress): Model\Location;
|
||||
}
|
||||
|
||||
80
module/Common/src/IpGeolocation/Model/Location.php
Normal file
80
module/Common/src/IpGeolocation/Model/Location.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\IpGeolocation\Model;
|
||||
|
||||
final class Location
|
||||
{
|
||||
/** @var string */
|
||||
private $countryCode;
|
||||
/** @var string */
|
||||
private $countryName;
|
||||
/** @var string */
|
||||
private $regionName;
|
||||
/** @var string */
|
||||
private $city;
|
||||
/** @var float */
|
||||
private $latitude;
|
||||
/** @var float */
|
||||
private $longitude;
|
||||
/** @var string */
|
||||
private $timeZone;
|
||||
|
||||
public function __construct(
|
||||
string $countryCode,
|
||||
string $countryName,
|
||||
string $regionName,
|
||||
string $city,
|
||||
float $latitude,
|
||||
float $longitude,
|
||||
string $timeZone
|
||||
) {
|
||||
$this->countryCode = $countryCode;
|
||||
$this->countryName = $countryName;
|
||||
$this->regionName = $regionName;
|
||||
$this->city = $city;
|
||||
$this->latitude = $latitude;
|
||||
$this->longitude = $longitude;
|
||||
$this->timeZone = $timeZone;
|
||||
}
|
||||
|
||||
public static function emptyInstance(): self
|
||||
{
|
||||
return new self('', '', '', '', 0.0, 0.0, '');
|
||||
}
|
||||
|
||||
public function countryCode(): string
|
||||
{
|
||||
return $this->countryCode;
|
||||
}
|
||||
|
||||
public function countryName(): string
|
||||
{
|
||||
return $this->countryName;
|
||||
}
|
||||
|
||||
public function regionName(): string
|
||||
{
|
||||
return $this->regionName;
|
||||
}
|
||||
|
||||
public function city(): string
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
public function latitude(): float
|
||||
{
|
||||
return $this->latitude;
|
||||
}
|
||||
|
||||
public function longitude(): float
|
||||
{
|
||||
return $this->longitude;
|
||||
}
|
||||
|
||||
public function timeZone(): string
|
||||
{
|
||||
return $this->timeZone;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user