<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\RosterRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\PrePersist;
use Doctrine\ORM\Mapping\PreUpdate;
use Symfony\Component\Serializer\Annotation\Groups;
#[ApiResource(
attributes: ['route_prefix' => 'v1'],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']],
)]
#[Entity(RosterRepository::class)]
#[HasLifecycleCallbacks]
class Roster
{
#[Id]
#[GeneratedValue]
#[Column('id', 'bigint', options: ['unsigned' => true])]
#[ApiProperty(identifier: true)]
#[Groups(['read'])]
private ?int $id = null;
#[ManyToOne(User::class)]
private ?User $sponsor = null;
#[Column('name', 'string')]
#[Groups(['read'])]
private ?string $name = null;
#[ManyToMany(Creator::class, inversedBy: 'rosters')]
private Collection $creators;
#[Column('created_at', 'datetime')]
private ?DateTime $createdAt = null;
#[Column('updated_at', 'datetime', nullable: true)]
private ?DateTime $updatedAt = null;
public function __construct()
{
$this->creators = new ArrayCollection();
}
#[PrePersist]
public function prePersist(): void
{
$this->createdAt = new DateTime();
}
#[PreUpdate]
public function preUpdate(): void
{
$this->updatedAt = new DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setSponsor(User $user): self
{
$this->sponsor = $user;
return $this;
}
public function getSponsor(): ?User
{
return $this->sponsor;
}
public function isSystem(): bool
{
return $this->getSponsor() === null;
}
public function getParameter(): string
{
return $this->isSystem()
? $this->getName()
: (string)$this->getId();
}
public function addCreator(Creator $creator): self
{
if (!$this->creators->contains($creator)) {
$this->creators->add($creator);
}
return $this;
}
public function removeCreator(Creator $creator): self
{
if ($this->creators->contains($creator)) {
$this->creators->removeElement($creator);
}
return $this;
}
public function getCreators(): Collection
{
return $this->creators;
}
public function setCreators(Collection $creators): self
{
$this->creators = $creators;
return $this;
}
}