<?php
namespace App\Entity;
use App\Entity\Traits\Sortable;
use App\Repository\LessonRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: LessonRepository::class)]
class Lesson
{
use Sortable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 100)]
private $name;
#[ORM\Column(type: 'string', length: 10)]
private $acronym;
#[ORM\Column(type: 'date_immutable', nullable: true)]
private $yearMin;
#[ORM\Column(type: 'date_immutable', nullable: true)]
private $yearMax;
#[ORM\Column(type: 'smallint', nullable: true)]
private $price;
#[ORM\Column(type: 'smallint')]
private $minSlot = 1;
#[ORM\Column(type: 'smallint')]
private $maxSlot = 1;
#[ORM\ManyToOne(targetEntity: Season::class, inversedBy: 'lessons')]
#[ORM\JoinColumn(nullable: false)]
private $season;
#[ORM\ManyToMany(targetEntity: Slot::class, mappedBy: 'lessons')]
private $slots;
#[ORM\OneToMany(targetEntity: Registration::class, mappedBy: 'lesson')]
private $registrations;
#[ORM\OneToMany(mappedBy: 'lesson', targetEntity: CartItemLesson::class)]
private $cartItemLessons;
public function __construct()
{
$this->slots = new ArrayCollection();
$this->registrations = new ArrayCollection();
$this->cartItemLessons = new ArrayCollection();
}
public function __toString(): string
{
return $this->getName();
}
public function getId(): ?int
{
return $this->id;
}
public function getName($full = false): ?string
{
if ($full) {
return sprintf('[%s] %s', $this->getAcronym(), $this->name);
}
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getAcronym(): ?string
{
return $this->acronym;
}
public function setAcronym(string $acronym): self
{
$this->acronym = $acronym;
return $this;
}
public function getYearMin(): ?\DateTimeImmutable
{
return $this->yearMin;
}
public function setYearMin(?\DateTimeImmutable $yearMin): self
{
$this->yearMin = $yearMin;
return $this;
}
public function getYearMax(): ?\DateTimeImmutable
{
return $this->yearMax;
}
public function setYearMax(?\DateTimeImmutable $yearMax): self
{
$this->yearMax = $yearMax;
return $this;
}
public function getPrice(): ?int
{
return $this->price ?? 0;
}
public function setPrice(?int $price): self
{
$this->price = $price;
return $this;
}
public function getMinSlot(): ?int
{
return $this->minSlot;
}
public function setMinSlot(int $minSlot): self
{
$this->minSlot = $minSlot;
return $this;
}
public function getMaxSlot(): ?int
{
return $this->maxSlot;
}
public function setMaxSlot(int $maxSlot): self
{
$this->maxSlot = $maxSlot;
return $this;
}
public function getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $season): self
{
$this->season = $season;
return $this;
}
/**
* @return Collection<int, Slot>
*/
public function getSlots(): Collection
{
return $this->slots;
}
public function addSlot(Slot $slot): self
{
if (!$this->slots->contains($slot)) {
$this->slots[] = $slot;
$slot->addLesson($this);
}
return $this;
}
public function removeSlot(Slot $slot): self
{
if ($this->slots->removeElement($slot)) {
$slot->removeLesson($this);
}
return $this;
}
/**
* @return Collection<int, Registration>
*/
public function getRegistrations(): Collection
{
return $this->registrations;
}
public function addRegistration(Registration $registration): self
{
if (!$this->registrations->contains($registration)) {
$this->registrations[] = $registration;
}
return $this;
}
public function removeRegistration(Registration $registration): self
{
$this->registrations->removeElement($registration);
return $this;
}
/**
* @return Collection<int, CartItemLesson>
*/
public function getCartItemLessons(): Collection
{
return $this->cartItemLessons;
}
public function addCartItemLesson(CartItemLesson $cartItemLesson): self
{
if (!$this->cartItemLessons->contains($cartItemLesson)) {
$this->cartItemLessons[] = $cartItemLesson;
$cartItemLesson->setLesson($this);
}
return $this;
}
public function removeCartItemLesson(CartItemLesson $cartItemLesson): self
{
if ($this->cartItemLessons->removeElement($cartItemLesson)) {
// set the owning side to null (unless already changed)
if ($cartItemLesson->getLesson() === $this) {
$cartItemLesson->setLesson(null);
}
}
return $this;
}
public function __clone()
{
$this->id = null;
$this->season = null;
$this->slots = new ArrayCollection();
$this->registrations = new ArrayCollection();
$this->cartItemLessons = new ArrayCollection();
$yearMin = \DateTime::createFromImmutable($this->yearMin);
$yearMax = \DateTime::createFromImmutable($this->yearMax);
$yearMin->add(new \DateInterval('P1Y'));
$yearMax->add(new \DateInterval('P1Y'));
$this->yearMin = \DateTimeImmutable::createFromMutable($yearMin);
$this->yearMax = \DateTimeImmutable::createFromMutable($yearMax);
}
}