<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-03-19
* Time: 20:14
*/
namespace App\Entity\Location;
use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
use App\Repository\DistrictRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* Район города
*/
#[ORM\Table(name: 'city_districts')]
#[ORM\Entity(repositoryClass: DistrictRepository::class)]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'geoposition')]
class District
{
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected int $id;
#[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: City::class, inversedBy: 'districts')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected City $city;
#[ORM\JoinColumn(name: 'county_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: County::class, inversedBy: 'districts')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected ?County $county;
#[ORM\Column(name: 'name', type: 'translatable')]
protected TranslatableValue $name;
#[ORM\Column(name: 'uri_identity', type: 'string', length: 128)]
protected string $uriIdentity;
/** @var Station[] */
#[ORM\OneToMany(targetEntity: Station::class, mappedBy: 'district')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected Collection $stations;
public function __construct(City $city, ?County $county, TranslatableValue $name, string $uriIdentity)
{
$this->relocate($city, $county);
$this->rename($name, $uriIdentity);
$this->stations = new ArrayCollection();
}
public function rename(TranslatableValue $name, string $uriIdentity): void
{
$this->name = $name;
$this->uriIdentity = $uriIdentity;
}
public function relocate(City $city, ?County $county): void
{
$this->city = $city;
$this->county = (null !== $county && $this->city->hasCounty($county)) ? $county : null;
}
public function getId(): int
{
return $this->id;
}
public function getCity(): City
{
return $this->city;
}
public function getCounty(): ?County
{
return $this->county;
}
public function getName(): TranslatableValue
{
return $this->name;
}
public function getUriIdentity(): string
{
return $this->uriIdentity;
}
/**
* @return Station[]
*/
public function getStations(): Collection
{
return $this->stations;
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return (string) $this->name;
}
}