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

111 lines
3 KiB
PHP
Raw Normal View History

2016-07-20 09:35:46 +02:00
<?php
2019-10-05 17:26:10 +02:00
2017-10-12 10:13:20 +02:00
declare(strict_types=1);
2016-07-20 09:35:46 +02:00
namespace Shlinkio\Shlink\Core\Entity;
2017-10-12 10:13:20 +02:00
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisitLocation;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
2016-07-20 09:35:46 +02:00
class VisitLocation extends AbstractEntity implements VisitLocationInterface
2016-07-20 09:35:46 +02:00
{
2019-12-29 22:27:00 +01:00
private string $countryCode;
private string $countryName;
private string $regionName;
private string $cityName;
private float $latitude;
private float $longitude;
2019-12-29 22:27:00 +01:00
private string $timezone;
2020-03-23 20:37:45 +01:00
private bool $isEmpty;
2016-07-20 09:35:46 +02:00
private function __construct()
2016-07-20 09:35:46 +02:00
{
}
public static function fromGeolocation(Location $location): self
{
$instance = new self();
$instance->countryCode = $location->countryCode();
$instance->countryName = $location->countryName();
$instance->regionName = $location->regionName();
$instance->cityName = $location->city();
$instance->latitude = $location->latitude();
$instance->longitude = $location->longitude();
$instance->timezone = $location->timeZone();
$instance->computeIsEmpty();
return $instance;
}
public static function fromImport(ImportedShlinkVisitLocation $location): self
{
$instance = new self();
$instance->countryCode = $location->countryCode();
$instance->countryName = $location->countryName();
$instance->regionName = $location->regionName();
$instance->cityName = $location->cityName();
$instance->latitude = $location->latitude();
$instance->longitude = $location->longitude();
$instance->timezone = $location->timeZone();
$instance->computeIsEmpty();
return $instance;
}
private function computeIsEmpty(): void
{
$this->isEmpty = (
$this->countryCode === '' &&
$this->countryName === '' &&
$this->regionName === '' &&
$this->cityName === '' &&
$this->latitude === 0.0 &&
$this->longitude === 0.0 &&
$this->timezone === ''
);
2016-07-20 09:35:46 +02:00
}
public function getCountryName(): string
2016-07-20 09:35:46 +02:00
{
return $this->countryName;
2016-07-20 09:35:46 +02:00
}
public function getLatitude(): float
2016-07-20 09:35:46 +02:00
{
return $this->latitude;
2016-07-20 09:35:46 +02:00
}
public function getLongitude(): float
2016-07-20 09:35:46 +02:00
{
return $this->longitude;
2016-07-20 09:35:46 +02:00
}
public function getCityName(): string
2016-07-20 09:35:46 +02:00
{
return $this->cityName;
2016-07-20 09:35:46 +02:00
}
2020-03-23 20:37:45 +01:00
public function isEmpty(): bool
{
return $this->isEmpty;
}
public function jsonSerialize(): array
2016-07-20 09:35:46 +02:00
{
return [
'countryCode' => $this->countryCode,
'countryName' => $this->countryName,
'regionName' => $this->regionName,
'cityName' => $this->cityName,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'timezone' => $this->timezone,
2020-03-23 20:37:45 +01:00
'isEmpty' => $this->isEmpty,
2016-07-20 09:35:46 +02:00
];
}
}