<?php
namespace App\Entity;
use App\Repository\SlotRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SlotRepository::class)]
class Slot
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'smallint', nullable: true)]
private $capacity;
#[ORM\Column(type: 'time_immutable')]
private $startAt;
#[ORM\Column(type: 'time_immutable')]
private $endAt;
#[ORM\Column(type: 'string', length: 20)]
private $weekDay;
#[ORM\Column(type: 'string', length: 50, nullable: true)]
private $zone;
#[ORM\Column(type: 'smallint')]
private $cost;
#[ORM\ManyToMany(targetEntity: Coach::class, inversedBy: 'slots')]
private $coachs;
#[ORM\ManyToMany(targetEntity: Lesson::class, inversedBy: 'slots')]
private $lessons;
#[ORM\ManyToMany(targetEntity: Registration::class, mappedBy: 'slots')]
private $registrations;
#[ORM\ManyToOne(targetEntity: Location::class, inversedBy: 'slots')]
#[ORM\JoinColumn(nullable: false)]
private $location;
#[ORM\ManyToOne(targetEntity: Season::class, inversedBy: 'slots')]
#[ORM\JoinColumn(nullable: false)]
private $season;
#[ORM\ManyToMany(targetEntity: Registration::class, mappedBy: 'proposedSlots')]
private $registrationPropositions;
public function __construct()
{
$this->coachs = new ArrayCollection();
$this->lessons = new ArrayCollection();
$this->registrations = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCapacity(): ?int
{
return $this->capacity;
}
public function setCapacity(?int $capacity): self
{
$this->capacity = $capacity;
return $this;
}
public function remainingCapacity(): int
{
return $this->getCapacity() - $this->getRegistrations()->count();
}
public function getStartAt(): ?\DateTimeImmutable
{
return $this->startAt;
}
public function setStartAt(\DateTimeImmutable $startAt): self
{
$this->startAt = $startAt;
return $this;
}
public function getEndAt(): ?\DateTimeImmutable
{
return $this->endAt;
}
public function setEndAt(\DateTimeImmutable $endAt): self
{
$this->endAt = $endAt;
return $this;
}
public function getWeekDay(): ?string
{
return $this->weekDay;
}
public function setWeekDay(string $weekDay): self
{
$this->weekDay = $weekDay;
return $this;
}
public function getZone(): ?string
{
return $this->zone;
}
public function setZone(?string $zone): self
{
$this->zone = $zone;
return $this;
}
public function getCost(): ?int
{
return $this->cost;
}
public function setCost(int $cost): self
{
$this->cost = $cost;
return $this;
}
/**
* @return Collection<int, Coach>
*/
public function getCoachs(): Collection
{
return $this->coachs;
}
public function addCoach(Coach $coach): self
{
if (!$this->coachs->contains($coach)) {
$this->coachs[] = $coach;
}
return $this;
}
public function removeCoach(Coach $coach): self
{
$this->coachs->removeElement($coach);
return $this;
}
/**
* @return Collection<int, Lesson>
*/
public function getLessons(): Collection
{
return $this->lessons;
}
public function addLesson(Lesson $lesson): self
{
if (!$this->lessons->contains($lesson)) {
$this->lessons[] = $lesson;
}
return $this;
}
public function removeLesson(Lesson $lesson): self
{
$this->lessons->removeElement($lesson);
return $this;
}
/**
* @return Collection<int, Registration>
*/
public function getRegistrations(?bool $proposed = false): Collection
{
$registrations = $this->registrations;
if ($proposed) {
foreach ($this->getRegistrationPropositions() as $registrationProposition) {
$this->registrations->add($registrationProposition);
}
}
return $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;
}
public function getLocation(): ?Location
{
return $this->location;
}
public function setLocation(?Location $location): self
{
$this->location = $location;
return $this;
}
public function __toString(): string
{
return sprintf(
'%s %s %s %s',
$this->getZone(),
$this->getWeekDay(),
$this->getStartAt()?->format('H:i'),
$this->getEndAt()?->format('H:i'),
);
}
public function getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $Season): self
{
$this->season = $Season;
return $this;
}
public function getName($showCapacity = false): string
{
$str = sprintf(
'%s / %s / %s-%s / %s',
$this->getLocation()->getName(),
$this->getWeekDay(),
$this->getStartAt()?->format('H:i'),
$this->getEndAt()?->format('H:i'),
$this->getZone(),
);
if ($showCapacity) {
$str = sprintf(
'%s [%s/%s]',
$str,
$this->getRegistrations()->count(),
$this->getCapacity()
);
}
return $str;
}
/**
* @return Collection<int, Registration>
*/
public function getRegistrationPropositions(): Collection
{
return $this->registrationPropositions;
}
public function __clone()
{
$this->id = null;
$this->coachs = new ArrayCollection();
$this->lessons = new ArrayCollection();
$this->registrations = new ArrayCollection();
$this->season = null;
$this->registrationPropositions = new ArrayCollection();
}
public function getSortableString(): string
{
$startAt = $this->getStartAt()?->format('His') ?? '000000';
$weekday = match ($this->getWeekDay()) {
'Lundi' => '01',
'Mardi' => '02',
'Mercredi' => '03',
'Jeudi' => '04',
'Vendredi' => '05',
'Samedi' => '06',
'Dimanche' => '07',
default => '00'
};
return sprintf("%s%s", $weekday, $startAt);
}
}