src/Entity/Course.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Entity\Route;
  5. use DateTime;
  6. use DateTimeImmutable;
  7. use DateTimeInterface;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * @ORM\Entity
  15.  */
  16. class Course
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private int $id;
  24.     /**
  25.      * @ORM\Column(type="string", length=255, nullable=false)
  26.      * @Assert\NotBlank
  27.      * @Assert\Length(max=255)
  28.      */
  29.     private string $name '';
  30.     /**
  31.      * @ORM\Column(type="string", length=255, nullable=false)
  32.      * @Assert\NotBlank
  33.      * @Assert\Length(max=255)
  34.      * @Assert\Regex(
  35.      *     pattern="/^[a-z0-9\p{L}]+$/iu",
  36.      *     message="CodeName can only contain letters (including Polish), numbers, and no spaces or special characters."
  37.      * )
  38.      */
  39.     private string $codeName;
  40.     /**
  41.      * @ORM\Column(type="datetime", nullable=false)
  42.      * @Assert\NotBlank
  43.      */
  44.     private DateTimeInterface $startTime;
  45.     /**
  46.      * @ORM\ManyToOne(targetEntity="Route", inversedBy="courses")
  47.      * @ORM\JoinColumn(nullable=false)
  48.      * @Assert\NotBlank
  49.      */
  50.     private Route $route;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity="BusOffer", mappedBy="course")
  53.      */
  54.     private Collection $offers;
  55.     public function __construct()
  56.     {
  57.         $this->offers = new ArrayCollection();
  58.     }
  59.     public function getId(): ?int
  60.     {
  61.         return $this->id;
  62.     }
  63.     public function getRoute(): ?Route
  64.     {
  65.         return $this->route;
  66.     }
  67.     public function setRoute(?Route $route): self
  68.     {
  69.         $this->route $route;
  70.         return $this;
  71.     }
  72.     public function getName(): ?string
  73.     {
  74.         return $this->name;
  75.     }
  76.     public function setName(string $name): self
  77.     {
  78.         $this->name $name;
  79.         return $this;
  80.     }
  81.     public function getCodeName(): ?string
  82.     {
  83.         return $this->codeName;
  84.     }
  85.     public function getFullCodeName(): string
  86.     {
  87.         return $this->route->getCodeName() . '_' $this->codeName;
  88.     }
  89.     public function setCodeName(string $codeName): self
  90.     {
  91.         $this->codeName $codeName;
  92.         return $this;
  93.     }
  94.     public function getStartTime(): DateTimeImmutable
  95.     {
  96.         return $this->startTime instanceof DateTime DateTimeImmutable::createFromMutable($this->startTime) : $this->startTime;
  97.     }
  98.     public function setStartTime(\DateTimeInterface $startTime): self
  99.     {
  100.         $this->startTime $startTime;
  101.         return $this;
  102.     }
  103.     /**
  104.      * @return Collection|BusOffer[]
  105.      */
  106.     public function getOffers()
  107.     {
  108.         return $this->offers;
  109.     }
  110.     public function addOffer(BusOffer $offer): self
  111.     {
  112.         if (!$this->offers->contains($offer)) {
  113.             $this->offers[] = $offer;
  114.             $offer->setCourse($this);
  115.         }
  116.         return $this;
  117.     }
  118.     public function removeOffer(BusOffer $offer): self
  119.     {
  120.         if ($this->offers->removeElement($offer)) {
  121.             // set the owning side to null (unless already changed)
  122.             if ($offer->getCourse() === $this) {
  123.                 $offer->setCourse(null);
  124.             }
  125.         }
  126.         return $this;
  127.     }
  128.     public function getApiName(): string
  129.     {
  130.         return $this->getRoute()->getCodeName() . '_' $this->getCodeName();
  131.     }
  132.     public function getFullName(): string
  133.     {
  134.         return $this->__toString();
  135.     }
  136.     public function __toString(): string
  137.     {
  138.         if ($this->getRoute() === null) {
  139.             return $this->getName() . ' (no route) #' $this->getId();
  140.         }
  141.         return $this->getRoute()?->getName() . ' (' $this->getName() . ')';
  142.     }
  143. }