<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-03-19
* Time: 17:12
*/
namespace App\Entity\Location;
use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
//use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\CityRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Index;
#[ORM\Table(name: 'cities')]
#[Index(name: 'idx_uri_identity', columns: ['uri_identity'])]
#[ORM\Entity(repositoryClass: CityRepository::class)]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'geoposition')]
class City
{
const GROUP_MEGALOPOLIS = 1;
const GROUP_MOSCOW_REGION = 2;
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[Groups('profile')]
protected int $id;
#[ORM\Column(name: 'name', type: 'translatable')]
#[Groups('profile')]
protected TranslatableValue $name;
#[ORM\Column(name: 'uri_identity', type: 'string', length: 128)]
#[Groups('profile')]
protected string $uriIdentity;
#[ORM\Column(name: 'country_code', type: 'string', length: 2)]
#[Groups('profile')]
protected string $countryCode;
#[ORM\Column(name: 'city_group', type: 'smallint', nullable: true)]
protected ?int $cityGroup;
/** @var County[] */
#[ORM\OneToMany(targetEntity: County::class, mappedBy: 'city')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected Collection $counties;
/** @var District[] */
#[ORM\OneToMany(targetEntity: District::class, mappedBy: 'city')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected Collection $districts;
/** @var Station[] */
#[ORM\OneToMany(targetEntity: Station::class, mappedBy: 'city')]
#[ORM\OrderBy(['name' => 'ASC'])]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected Collection $stations;
#[ORM\Embedded(class: MapCoordinate::class, columnPrefix: false)]
protected ?MapCoordinate $mapCoordinate;
#[ORM\Column(name: 'timezone', type: 'string', length: 6, nullable: true)]
protected ?string $timezone;
public function __construct(TranslatableValue $name, string $uriIdentity, string $countryCode, ?int $cityGroup = null, ?MapCoordinate $mapCoordinate = null)
{
$this->rename($name, $uriIdentity);
$this->relocate($countryCode, $cityGroup, $mapCoordinate);
$this->counties = new ArrayCollection();
$this->districts = new ArrayCollection();
$this->stations = new ArrayCollection();
}
/**
* @param City|string $cityOrUriIdentity Instance of City class or URI identity string
*/
public function equals(City|string $cityOrUriIdentity): bool
{
if ($cityOrUriIdentity instanceof City) {
return $cityOrUriIdentity->id === $this->id;
} elseif (is_string($cityOrUriIdentity)) {
return $cityOrUriIdentity === $this->uriIdentity;
}
throw new \InvalidArgumentException(__METHOD__.' can accept either string with URI identity or instance of App\\Entity\\Location\\City class.');
}
public function rename(TranslatableValue $name, string $uriIdentity): void
{
$this->name = $name;
$this->uriIdentity = $uriIdentity;
}
public function relocate(string $countryCode, ?int $cityGroup, ?MapCoordinate $mapCoordinate): void
{
$this->countryCode = $countryCode;
$this->cityGroup = $cityGroup;
$this->mapCoordinate = $mapCoordinate;
}
public function getId(): int
{
return $this->id;
}
public function getName(): TranslatableValue
{
return $this->name;
}
public function getUriIdentity(): string
{
return $this->uriIdentity;
}
public function getCountryCode(): string
{
return $this->countryCode;
}
public function getCityGroup(): ?int
{
return $this->cityGroup;
}
/**
* @return County[]
*/
public function getCounties(): Collection
{
return $this->counties;
}
public function hasCounty(County $county): bool
{
return $this->counties->contains($county);
}
/**
* @return District[]
*/
public function getDistricts(): Collection
{
return $this->districts;
}
public function hasDistrict(District $district): bool
{
return $this->districts->contains($district);
}
/**
* @return Station[]
*/
public function getStations(): Collection
{
return $this->stations;
}
public function hasStation(Station $station): bool
{
return $this->stations->contains($station);
}
//TODO return type?
public function getMapCoordinate(): ?MapCoordinate
{
return $this->mapCoordinate;
}
public function getTimezone(): ?string
{
return $this->timezone;
}
public function setTimezone(string $timezone): void
{
$this->timezone = $timezone;
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return (string) $this->name;
}
}