shlink/module/Core/src/Entity/VisitLocation.php

89 lines
2.3 KiB
PHP
Raw Normal View History

2016-07-20 10:35:46 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
2016-07-20 10:35:46 +03:00
namespace Shlinkio\Shlink\Core\Entity;
2017-10-12 11:13:20 +03:00
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
2016-07-20 10:35:46 +03:00
class VisitLocation extends AbstractEntity implements VisitLocationInterface
2016-07-20 10:35:46 +03:00
{
/** @var string */
private $countryCode;
/** @var string */
private $countryName;
/** @var string */
private $regionName;
/** @var string */
private $cityName;
/** @var string */
private $latitude;
/** @var string */
private $longitude;
/** @var string */
private $timezone;
2016-07-20 10:35:46 +03:00
public function __construct(Location $location)
2016-07-20 10:35:46 +03:00
{
$this->exchangeLocationInfo($location);
2016-07-20 10:35:46 +03:00
}
public function getCountryName(): string
2016-07-20 10:35:46 +03:00
{
return $this->countryName ?? '';
2016-07-20 10:35:46 +03:00
}
public function getLatitude(): string
2016-07-20 10:35:46 +03:00
{
return $this->latitude ?? '';
2016-07-20 10:35:46 +03:00
}
public function getLongitude(): string
2016-07-20 10:35:46 +03:00
{
return $this->longitude ?? '';
2016-07-20 10:35:46 +03:00
}
public function getCityName(): string
2016-07-20 10:35:46 +03:00
{
return $this->cityName ?? '';
2016-07-20 10:35:46 +03:00
}
private function exchangeLocationInfo(Location $info): void
2016-07-20 10:35:46 +03:00
{
$this->countryCode = $info->countryCode();
$this->countryName = $info->countryName();
$this->regionName = $info->regionName();
$this->cityName = $info->city();
$this->latitude = (string) $info->latitude();
$this->longitude = (string) $info->longitude();
$this->timezone = $info->timeZone();
2016-07-20 10:35:46 +03:00
}
public function jsonSerialize(): array
2016-07-20 10:35:46 +03:00
{
return [
'countryCode' => $this->countryCode,
'countryName' => $this->countryName,
'regionName' => $this->regionName,
'cityName' => $this->cityName,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'timezone' => $this->timezone,
];
}
public function isEmpty(): bool
{
return
$this->countryCode === '' &&
$this->countryName === '' &&
$this->regionName === '' &&
$this->cityName === '' &&
((float) $this->latitude) === 0.0 &&
((float) $this->longitude) === 0.0 &&
$this->timezone === '';
}
2016-07-20 10:35:46 +03:00
}