<?php
declare(strict_types=1);
namespace App\Entity;
use App\Entity\Route;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*/
class Course
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
* @Assert\NotBlank
* @Assert\Length(max=255)
*/
private string $name = '';
/**
* @ORM\Column(type="string", length=255, nullable=false)
* @Assert\NotBlank
* @Assert\Length(max=255)
* @Assert\Regex(
* pattern="/^[a-z0-9\p{L}]+$/iu",
* message="CodeName can only contain letters (including Polish), numbers, and no spaces or special characters."
* )
*/
private string $codeName;
/**
* @ORM\Column(type="datetime", nullable=false)
* @Assert\NotBlank
*/
private DateTimeInterface $startTime;
/**
* @ORM\ManyToOne(targetEntity="Route", inversedBy="courses")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank
*/
private Route $route;
/**
* @ORM\OneToMany(targetEntity="BusOffer", mappedBy="course")
*/
private Collection $offers;
public function __construct()
{
$this->offers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getRoute(): ?Route
{
return $this->route;
}
public function setRoute(?Route $route): self
{
$this->route = $route;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCodeName(): ?string
{
return $this->codeName;
}
public function getFullCodeName(): string
{
return $this->route->getCodeName() . '_' . $this->codeName;
}
public function setCodeName(string $codeName): self
{
$this->codeName = $codeName;
return $this;
}
public function getStartTime(): DateTimeImmutable
{
return $this->startTime instanceof DateTime ? DateTimeImmutable::createFromMutable($this->startTime) : $this->startTime;
}
public function setStartTime(\DateTimeInterface $startTime): self
{
$this->startTime = $startTime;
return $this;
}
/**
* @return Collection|BusOffer[]
*/
public function getOffers()
{
return $this->offers;
}
public function addOffer(BusOffer $offer): self
{
if (!$this->offers->contains($offer)) {
$this->offers[] = $offer;
$offer->setCourse($this);
}
return $this;
}
public function removeOffer(BusOffer $offer): self
{
if ($this->offers->removeElement($offer)) {
// set the owning side to null (unless already changed)
if ($offer->getCourse() === $this) {
$offer->setCourse(null);
}
}
return $this;
}
public function getApiName(): string
{
return $this->getRoute()->getCodeName() . '_' . $this->getCodeName();
}
public function getFullName(): string
{
return $this->__toString();
}
public function __toString(): string
{
if ($this->getRoute() === null) {
return $this->getName() . ' (no route) #' . $this->getId();
}
return $this->getRoute()?->getName() . ' (' . $this->getName() . ')';
}
}