<?php
namespace App\Entity;
use App\Entity\Traits\TimeStampable;
use App\Repository\DocumentTypeRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: DocumentTypeRepository::class)]
#[UniqueEntity(
fields: ['label',],
errorPath: 'label',
message: 'Le nom que vous avez saisi est déjà utilisé',
)]
#[ORM\HasLifecycleCallbacks]
class DocumentType
{
use TimeStampable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(["entity:write", "documentProjet:read","documentType:read"])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(["entity:write", "documentProjet:read", "documentType:read"])]
#[Assert\NotBlank(message:"Veuillez remplir ce champ")]
private ?string $label = null;
#[ORM\Column(length: 100)]
#[Groups(["entity:write", "documentType:read"])]
private ?string $category = null;
#[ORM\ManyToOne]
private ?User $createdBy = null;
#[ORM\OneToMany(mappedBy: 'typeDocument', targetEntity: Document::class)]
private Collection $documents;
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
$this->updatedAt = new DateTimeImmutable();
$this->documents = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getCategory(): ?string
{
return $this->category;
}
public function setCategory(string $category): self
{
$this->category = $category;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
/**
* @return Collection<int, Document>
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): static
{
if (!$this->documents->contains($document)) {
$this->documents->add($document);
$document->setTypeDocument($this);
}
return $this;
}
public function removeDocument(Document $document): static
{
if ($this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if ($document->getTypeDocument() === $this) {
$document->setTypeDocument(null);
}
}
return $this;
}
public function __toString(): string
{
return $this->label;
}
}