Files
myep-team/src/Entity/Availability.php
T

154 lines
3.8 KiB
PHP

<?php
namespace App\Entity;
use App\Entity\Traits\BlameableEntity;
use App\Entity\Traits\SoftDeletableEntity;
use App\Entity\Traits\TimestampableEntity;
use App\Repository\AvailabilityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: AvailabilityRepository::class)]
#[UniqueEntity(
fields: ['dateFrom', 'dateTo'],
message: 'Eine Verfügbarkeit für diesen Zeitraum ist bereits vorhanden',
repositoryMethod: 'assertUnique'
)]
class Availability implements BlameableEntityInterface, TimestampableEntityInterface, SoftDeletableEntityInterface
{
use BlameableEntity;
use TimestampableEntity;
use SoftDeletableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
#[Assert\NotNull(message: 'Bitte gib das Datum an')]
private ?\DateTimeImmutable $dateFrom = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
#[Assert\NotNull(message: 'Bitte gib das Datum an')]
#[Assert\GreaterThan(propertyPath: 'dateFrom', message: 'Das Datum muss nach dem Beginndatum liegen')]
private ?\DateTimeImmutable $dateTo = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\ManyToOne(inversedBy: 'customAvailabilities')]
private ?Teamer $owner = null;
#[ORM\ManyToMany(targetEntity: Teamer::class, mappedBy: 'availabilities')]
private Collection $teamers;
public function __construct()
{
$this->teamers = new ArrayCollection();
}
public static function duplicate(Availability $availability): static
{
$instance = new static();
$instance
->setDateFrom($availability->getDateFrom())
->setDateTo($availability->getDateTo())
->setDescription($availability->getDescription())
;
return $instance;
}
public function __toString()
{
return sprintf('%s - %s', $this->getDateFrom()->format('d.m.Y'), $this->getDateTo()->format('d.m.Y'));
}
public function getId(): ?int
{
return $this->id;
}
public function getDateFrom(): ?\DateTimeImmutable
{
return $this->dateFrom;
}
public function setDateFrom(?\DateTimeImmutable $dateFrom): static
{
$this->dateFrom = $dateFrom;
return $this;
}
public function getDateTo(): ?\DateTimeImmutable
{
return $this->dateTo;
}
public function setDateTo(?\DateTimeImmutable $dateTo): static
{
$this->dateTo = $dateTo;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getOwner(): ?Teamer
{
return $this->owner;
}
public function setOwner(?Teamer $owner): static
{
$this->owner = $owner;
return $this;
}
/**
* @return Collection<int, Teamer>
*/
public function getTeamers(): Collection
{
return $this->teamers;
}
public function addTeamer(Teamer $teamer): static
{
if (!$this->teamers->contains($teamer)) {
$this->teamers->add($teamer);
$teamer->addAvailability($this);
}
return $this;
}
public function removeTeamer(Teamer $teamer): static
{
if ($this->teamers->removeElement($teamer)) {
$teamer->removeAvailability($this);
}
return $this;
}
}