src/Entity/User.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\UserRepository;
  5. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  6. use App\Traits\UuidEntityTrait;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use OpenApi\Annotations as OA;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13.  * @ORM\Entity(repositoryClass=UserRepository::class)
  14.  * @ORM\Table(name="`user`")
  15.  */
  16. class User implements UserInterfacePasswordAuthenticatedUserInterface
  17. {
  18.     use UuidEntityTrait;
  19.     /**
  20.      * @var string The hashed password
  21.      * @ORM\Column(type="string")
  22.      */
  23.     private string $password;
  24.     /**
  25.      * @ORM\Column(type="json")
  26.      * @OA\Property(type="array", @OA\Items(type="string"))
  27.      */
  28.     private array $roles = [];
  29.     /**
  30.      * @ORM\OneToMany(targetEntity=Order::class, mappedBy="user")
  31.      */
  32.     private Collection $orders;
  33.     public function __construct()
  34.     {
  35.         $this->orders = new ArrayCollection();
  36.     }
  37.     /**
  38.      * A visual identifier that represents this user.
  39.      *
  40.      * @see UserInterface
  41.      */
  42.     public function getUserIdentifier(): string
  43.     {
  44.         return (string) $this->id;
  45.     }
  46.     /**
  47.      * A visual identifier that represents this user.
  48.      *
  49.      * @see UserInterface
  50.      */
  51.     public function getUsername(): string
  52.     {
  53.         return $this->getUserIdentifier();
  54.     }
  55.     /**
  56.      * @see UserInterface
  57.      */
  58.     public function getRoles(): array
  59.     {
  60.         $roles $this->roles;
  61.         $roles[] = 'ROLE_USER';
  62.         return \array_unique($roles);
  63.     }
  64.     public function setRoles(array $roles): self
  65.     {
  66.         $this->roles $roles;
  67.         return $this;
  68.     }
  69.     /**
  70.      * @see UserInterface
  71.      */
  72.     public function getPassword(): string
  73.     {
  74.         return $this->password;
  75.     }
  76.     public function setPassword(string $password): self
  77.     {
  78.         $this->password $password;
  79.         return $this;
  80.     }
  81.     /**
  82.      * Returning a salt is only needed, if you are not using a modern
  83.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  84.      *
  85.      * @see UserInterface
  86.      */
  87.     public function getSalt(): ?string
  88.     {
  89.         return null;
  90.     }
  91.     /**
  92.      * @see UserInterface
  93.      */
  94.     public function eraseCredentials()
  95.     {
  96.     }
  97.     public function getOrders(): Collection
  98.     {
  99.         return $this->orders;
  100.     }
  101.     public function addOrder(Order $order): self
  102.     {
  103.         if (!$this->orders->contains($order)) {
  104.             $this->orders[] = $order;
  105.             $order->setUser($this);
  106.         }
  107.         return $this;
  108.     }
  109.     public function removeOrder(Order $order): self
  110.     {
  111.         if ($this->orders->removeElement($order)) {
  112.             // set the owning side to null (unless already changed)
  113.             if ($order->getUser() === $this) {
  114.                 $order->setUser(null);
  115.             }
  116.         }
  117.         return $this;
  118.     }
  119.     public function __toString()
  120.     {
  121.         return $this->id?->toRfc4122() ?? ' ';
  122.     }
  123. }