<?php
namespace App\Entity;
use App\Repository\UserRepository;
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\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity('email', message: "Cet e-mail existe déja")]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(["getTimes", "getReservation"])]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Groups(["getTimes"])]
#[Assert\NotBlank]
#[Assert\Email]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 45)]
#[Groups(["getTimes", "getReservation"])]
#[Assert\NotBlank]
#[Assert\Length(min: 2, max: 45)]
private ?string $firstName = null;
#[ORM\Column(length: 45)]
#[Groups(["getTimes", "getReservation"])]
#[Assert\NotBlank]
private ?string $lastName = null;
#[ORM\Column]
#[Groups(["getTimes"])]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\ManyToMany(targetEntity: Project::class, inversedBy: 'users')]
private Collection $projects;
#[ORM\ManyToMany(targetEntity: Client::class, inversedBy: 'users')]
private Collection $clients;
#[ORM\OneToMany(mappedBy: 'employee', targetEntity: Reservation::class)]
private Collection $reservation;
#[ORM\OneToMany(mappedBy: 'employee', targetEntity: Absence::class, orphanRemoval: true)]
private Collection $absences;
#[ORM\Column(type: 'boolean', options: ['default' => 0])]
private bool $archived = false;
public function __construct()
{
$this->projects = new ArrayCollection();
$this->clients = new ArrayCollection();
$this->reservation = new ArrayCollection();
$this->absences = new ArrayCollection();
}
public function __toString(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string)$this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection<int, Project>
*/
public function getProjects(): Collection
{
return $this->projects;
}
public function addProject(Project $project): self
{
if (!$this->projects->contains($project)) {
$this->projects->add($project);
}
return $this;
}
public function removeProject(Project $project): self
{
$this->projects->removeElement($project);
return $this;
}
/**
* @return Collection<int, Client>
*/
public function getClient(): Collection
{
return $this->clients;
}
public function addClient(Client $client): self
{
if (!$this->clients->contains($client)) {
$this->clients->add($client);
}
return $this;
}
public function removeClient(Client $client): self
{
$this->clients->removeElement($client);
return $this;
}
/**
* @return Collection<int, Reservation>
*/
public function getReservation(): Collection
{
return $this->reservation;
}
public function addReservation(Reservation $reservation): self
{
if (!$this->reservation->contains($reservation)) {
$this->reservation->add($reservation);
$reservation->setEmployee($this);
}
return $this;
}
public function removeReservation(Reservation $reservation): self
{
if ($this->reservation->removeElement($reservation)) {
// set the owning side to null (unless already changed)
if ($reservation->getEmployee() === $this) {
$reservation->setEmployee(null);
}
}
return $this;
}
/**
* @return Collection<int, Absence>
*/
public function getAbsences(): Collection
{
return $this->absences;
}
public function addAbsence(Absence $absence): self
{
if (!$this->absences->contains($absence)) {
$this->absences->add($absence);
$absence->setEmployee($this);
}
return $this;
}
public function removeAbsence(Absence $absence): self
{
if ($this->absences->removeElement($absence)) {
// set the owning side to null (unless already changed)
if ($absence->getEmployee() === $this) {
$absence->setEmployee(null);
}
}
return $this;
}
public function getFullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function isArchived(): ?bool
{
return $this->archived;
}
public function setArchived(bool $archived): static
{
$this->archived = $archived;
return $this;
}
}