src/Entity/Administrator.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\AdministratorRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. #[ORM\Entity(repositoryClassAdministratorRepository::class)]
  11. #[ApiResource]
  12. class Administrator implements UserInterfacePasswordAuthenticatedUserInterface
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column]
  17.     private ?int $id null;
  18.     #[ORM\Column(length180uniquetrue)]
  19.     private ?string $username null;
  20.     #[ORM\Column]
  21.     private array $roles = [];
  22.     #[ORM\Embedded(class: UserInformation::class)]
  23.     private UserInformation $userInformation;
  24.     /**
  25.      * @var string The hashed password
  26.      */
  27.     #[ORM\Column]
  28.     private ?string $password null;
  29.     #[ORM\ManyToMany(targetEntityAdministratorGroup::class, mappedBy'administrators')]
  30.     private Collection $administratorGroups;
  31.     public function __construct()
  32.     {
  33.         $this->userInformation = new UserInformation();
  34.         $this->getUserInformation()->setCreatedAt(new \DateTimeImmutable());
  35.         $this->getUserInformation()->setBirthday(new \DateTime());
  36.         $this->administratorGroups = new ArrayCollection();
  37.     }
  38.     public function __toString(): string
  39.     {
  40.         return $this->getUserInformation()->getFirstName().' '.$this->getUserInformation()->getLastName();
  41.     }
  42.     public function getId(): ?int
  43.     {
  44.         return $this->id;
  45.     }
  46.     public function getUsername(): ?string
  47.     {
  48.         return $this->username;
  49.     }
  50.     public function setUsername(string $username): self
  51.     {
  52.         $this->username $username;
  53.         return $this;
  54.     }
  55.     /**
  56.      * A visual identifier that represents this user.
  57.      *
  58.      * @see UserInterface
  59.      */
  60.     public function getUserIdentifier(): string
  61.     {
  62.         return (string)$this->username;
  63.     }
  64.     /**
  65.      * @see UserInterface
  66.      */
  67.     public function getRoles(): array
  68.     {
  69.         $roles $this->roles;
  70.         // guarantee every user at least has ROLE_USER
  71.         $roles[] = 'ROLE_USER';
  72.         return array_unique($roles);
  73.     }
  74.     public function setRoles(array $roles): self
  75.     {
  76.         $this->roles $roles;
  77.         return $this;
  78.     }
  79.     /**
  80.      * @see PasswordAuthenticatedUserInterface
  81.      */
  82.     public function getPassword(): string
  83.     {
  84.         return $this->password;
  85.     }
  86.     public function setPassword(string $password): self
  87.     {
  88.         $this->password $password;
  89.         return $this;
  90.     }
  91.     /**
  92.      * @see UserInterface
  93.      */
  94.     public function eraseCredentials()
  95.     {
  96.         // If you store any temporary, sensitive data on the user, clear it here
  97.         // $this->plainPassword = null;
  98.     }
  99.     public function getUserInformation(): UserInformation
  100.     {
  101.         return $this->userInformation;
  102.     }
  103.     public function setUserInformation(UserInformation $userInformation): self
  104.     {
  105.         $this->userInformation $userInformation;
  106.         return $this;
  107.     }
  108.     /**
  109.      * @return Collection<int, AdministratorGroup>
  110.      */
  111.     public function getAdministratorGroups(): Collection
  112.     {
  113.         return $this->administratorGroups;
  114.     }
  115.     public function addAdministratorGroup(AdministratorGroup $administratorGroup): self
  116.     {
  117.         if (!$this->administratorGroups->contains($administratorGroup)) {
  118.             $this->administratorGroups->add($administratorGroup);
  119.             $administratorGroup->addAdministrator($this);
  120.         }
  121.         return $this;
  122.     }
  123.     public function removeAdministratorGroup(AdministratorGroup $administratorGroup): self
  124.     {
  125.         if ($this->administratorGroups->removeElement($administratorGroup)) {
  126.             $administratorGroup->removeAdministrator($this);
  127.         }
  128.         return $this;
  129.     }
  130. }