<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-03-19
* Time: 17:13
*/
namespace App\Entity\Location;
use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Location\Subway\Line;
use App\Repository\StationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Table(name: 'city_stations')]
#[ORM\Entity(repositoryClass: StationRepository::class)]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'geoposition')]
class Station
{
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[Groups('profile')]
protected int $id;
#[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: City::class, inversedBy: 'stations')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected City $city;
#[ORM\JoinColumn(name: 'county_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: County::class, inversedBy: 'stations')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected ?County $county;
#[ORM\JoinColumn(name: 'district_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: District::class, inversedBy: 'stations')]
#[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
protected ?District $district;
#[ORM\JoinTable(name: 'city_subway_station_lines')]
#[ORM\JoinColumn(name: 'station_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'line_id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: Line::class, fetch: 'EXTRA_LAZY')]
protected Collection $lines;
#[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;
public function __construct(City $city, ?County $county, ?District $district, TranslatableValue $name, string $uriIdentity)
{
$this->city = $city;
$this->county = $county;
$this->district = $district;
$this->name = $name;
$this->uriIdentity = $uriIdentity;
$this->lines = new ArrayCollection();
}
public function rename(TranslatableValue $name, string $uriIdentity): void
{
$this->name = $name;
$this->uriIdentity = $uriIdentity;
}
public function relocate(City $city, ?County $county, ?District $district): void
{
$this->city = $city;
$this->county = (null !== $county && $this->city->hasCounty($county)) ? $county : null;
$this->district = (
null !== $district
&& $this->city->hasDistrict($district)
&& (null === $this->county || $this->county->hasDistrict($district))
) ? $district : null;
}
public function getId(): int
{
return $this->id;
}
public function getCity(): City
{
return $this->city;
}
public function getCounty(): ?County
{
return $this->county;
}
public function getDistrict(): ?District
{
return $this->district;
}
public function getName(): TranslatableValue
{
return $this->name;
}
public function getUriIdentity(): string
{
return $this->uriIdentity;
}
public function setDistrict(?District $district): void
{
$this->district = $district;
}
public function lines(): iterable
{
return $this->lines;
}
public function specifyLines(Line ...$lines): void
{
$this->lines = new ArrayCollection($lines);
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return (string) $this->name;
}
}