src/Entity/Roster.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Repository\RosterRepository;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping\Column;
  11. use Doctrine\ORM\Mapping\Entity;
  12. use Doctrine\ORM\Mapping\GeneratedValue;
  13. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  14. use Doctrine\ORM\Mapping\Id;
  15. use Doctrine\ORM\Mapping\ManyToMany;
  16. use Doctrine\ORM\Mapping\ManyToOne;
  17. use Doctrine\ORM\Mapping\PrePersist;
  18. use Doctrine\ORM\Mapping\PreUpdate;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. #[ApiResource(
  21.     attributes: ['route_prefix' => 'v1'],
  22.     denormalizationContext: ['groups' => ['write']],
  23.     normalizationContext: ['groups' => ['read']],
  24. )]
  25. #[Entity(RosterRepository::class)]
  26. #[HasLifecycleCallbacks]
  27. class Roster
  28. {
  29.     #[Id]
  30.     #[GeneratedValue]
  31.     #[Column('id''bigint'options: ['unsigned' => true])]
  32.     #[ApiProperty(identifiertrue)]
  33.     #[Groups(['read'])]
  34.     private ?int $id null;
  35.     #[ManyToOne(User::class)]
  36.     private ?User $sponsor null;
  37.     #[Column('name''string')]
  38.     #[Groups(['read'])]
  39.     private ?string $name null;
  40.     #[ManyToMany(Creator::class, inversedBy'rosters')]
  41.     private Collection $creators;
  42.     #[Column('created_at''datetime')]
  43.     private ?DateTime $createdAt null;
  44.     #[Column('updated_at''datetime'nullabletrue)]
  45.     private ?DateTime $updatedAt null;
  46.     public function __construct()
  47.     {
  48.         $this->creators = new ArrayCollection();
  49.     }
  50.     #[PrePersist]
  51.     public function prePersist(): void
  52.     {
  53.         $this->createdAt = new DateTime();
  54.     }
  55.     #[PreUpdate]
  56.     public function preUpdate(): void
  57.     {
  58.         $this->updatedAt = new DateTime();
  59.     }
  60.     public function getId(): ?int
  61.     {
  62.         return $this->id;
  63.     }
  64.     public function setName(string $name): self
  65.     {
  66.         $this->name $name;
  67.         return $this;
  68.     }
  69.     public function getName(): ?string
  70.     {
  71.         return $this->name;
  72.     }
  73.     public function setSponsor(User $user): self
  74.     {
  75.         $this->sponsor $user;
  76.         return $this;
  77.     }
  78.     public function getSponsor(): ?User
  79.     {
  80.         return $this->sponsor;
  81.     }
  82.     public function isSystem(): bool
  83.     {
  84.         return $this->getSponsor() === null;
  85.     }
  86.     public function getParameter(): string
  87.     {
  88.         return $this->isSystem()
  89.             ? $this->getName()
  90.             : (string)$this->getId();
  91.     }
  92.     public function addCreator(Creator $creator): self
  93.     {
  94.         if (!$this->creators->contains($creator)) {
  95.             $this->creators->add($creator);
  96.         }
  97.         return $this;
  98.     }
  99.     public function removeCreator(Creator $creator): self
  100.     {
  101.         if ($this->creators->contains($creator)) {
  102.             $this->creators->removeElement($creator);
  103.         }
  104.         return $this;
  105.     }
  106.     public function getCreators(): Collection
  107.     {
  108.         return $this->creators;
  109.     }
  110.     public function setCreators(Collection $creators): self
  111.     {
  112.         $this->creators $creators;
  113.         return $this;
  114.     }
  115. }