src/Entity/User.php line 16

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. #[ORM\Entity(UserRepository::class)]
  12. #[ORM\Table('`user`')]
  13. class User implements UserInterfacePasswordAuthenticatedUserInterface
  14. {
  15.     use TimestampableEntity;
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     private ?int $id;
  20.     #[ORM\Column(length180uniquetrue)]
  21.     private ?string $email;
  22.     #[ORM\Column(typeTypes::JSON)]
  23.     private array $roles = [];
  24.     /**
  25.      * The hashed password
  26.      */
  27.     #[ORM\Column]
  28.     private ?string $password;
  29.     /**
  30.      * The plain non-persisted password
  31.      */
  32.     private ?string $plainPassword;
  33.     #[ORM\Column]
  34.     private bool $enabled true;
  35.     #[ORM\Column]
  36.     private ?string $firstName;
  37.     #[ORM\Column]
  38.     private ?string $lastName;
  39.     #[ORM\Column(nullabletrue)]
  40.     private ?string $avatar;
  41.     #[ORM\OneToMany('askedBy'Question::class)]
  42.     private Collection $questions;
  43.     #[ORM\OneToMany('answeredBy'Answer::class)]
  44.     private Collection $answers;
  45.     #[ORM\OneToMany(mappedBy'user'targetEntityComment::class, orphanRemovaltrue)]
  46.     private Collection $comments;
  47.     public function __construct()
  48.     {
  49.         $this->questions = new ArrayCollection();
  50.         $this->answers = new ArrayCollection();
  51.         $this->comments = new ArrayCollection();
  52.     }
  53.     public function __toString(): string
  54.     {
  55.         return $this->getFullName();
  56.     }
  57.     public function getId(): ?int
  58.     {
  59.         return $this->id;
  60.     }
  61.     public function getEmail(): ?string
  62.     {
  63.         return $this->email;
  64.     }
  65.     public function setEmail(string $email): self
  66.     {
  67.         $this->email $email;
  68.         return $this;
  69.     }
  70.     /**
  71.      * A visual identifier that represents this user.
  72.      *
  73.      * @see UserInterface
  74.      */
  75.     public function getUserIdentifier(): string
  76.     {
  77.         return (string) $this->email;
  78.     }
  79.     /**
  80.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  81.      */
  82.     public function getUsername(): string
  83.     {
  84.         return (string) $this->email;
  85.     }
  86.     /**
  87.      * @see UserInterface
  88.      */
  89.     public function getRoles(): array
  90.     {
  91.         $roles $this->roles;
  92.         // guarantee every user at least has ROLE_USER
  93.         $roles[] = 'ROLE_USER';
  94.         return array_unique($roles);
  95.     }
  96.     public function setRoles(array $roles): self
  97.     {
  98.         $this->roles $roles;
  99.         return $this;
  100.     }
  101.     /**
  102.      * @see PasswordAuthenticatedUserInterface
  103.      */
  104.     public function getPassword(): string
  105.     {
  106.         return $this->password;
  107.     }
  108.     public function setPassword(string $password): self
  109.     {
  110.         $this->password $password;
  111.         return $this;
  112.     }
  113.     public function getPlainPassword(): string
  114.     {
  115.         return $this->plainPassword;
  116.     }
  117.     public function setPlainPassword(string $plainPassword): void
  118.     {
  119.         $this->plainPassword $plainPassword;
  120.     }
  121.     /**
  122.      * Returning a salt is only needed, if you are not using a modern
  123.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  124.      *
  125.      * @see UserInterface
  126.      */
  127.     public function getSalt(): ?string
  128.     {
  129.         return null;
  130.     }
  131.     /**
  132.      * @see UserInterface
  133.      */
  134.     public function eraseCredentials()
  135.     {
  136.         // If you store any temporary, sensitive data on the user, clear it here
  137.          $this->plainPassword null;
  138.     }
  139.     public function isEnabled(): bool
  140.     {
  141.         return $this->enabled;
  142.     }
  143.     public function setEnabled(bool $enabled): void
  144.     {
  145.         $this->enabled $enabled;
  146.     }
  147.     public function getFirstName(): ?string
  148.     {
  149.         return $this->firstName;
  150.     }
  151.     public function setFirstName(string $firstName): void
  152.     {
  153.         $this->firstName $firstName;
  154.     }
  155.     public function getLastName(): ?string
  156.     {
  157.         return $this->lastName;
  158.     }
  159.     public function setLastName(string $lastName): void
  160.     {
  161.         $this->lastName $lastName;
  162.     }
  163.     public function getFullName(): ?string
  164.     {
  165.         return $this->firstName.' '.$this->lastName;
  166.     }
  167.     public function getAvatar(): ?string
  168.     {
  169.         return $this->avatar;
  170.     }
  171.     public function getAvatarUrl(): ?string
  172.     {
  173.         if (!$this->avatar) {
  174.             return null;
  175.         }
  176.         if (strpos($this->avatar'/') !== false) {
  177.             return $this->avatar;
  178.         }
  179.         return sprintf('/uploads/avatars/%s'$this->avatar);
  180.     }
  181.     public function setAvatar(?string $avatar): void
  182.     {
  183.         $this->avatar $avatar;
  184.     }
  185.     /**
  186.      * @return Collection|Question[]
  187.      */
  188.     public function getQuestions(): Collection
  189.     {
  190.         return $this->questions;
  191.     }
  192.     public function addQuestion(Question $question): self
  193.     {
  194.         if (!$this->questions->contains($question)) {
  195.             $this->questions[] = $question;
  196.             $question->setAskedBy($this);
  197.         }
  198.         return $this;
  199.     }
  200.     public function removeQuestion(Question $question): self
  201.     {
  202.         if ($this->questions->removeElement($question)) {
  203.             // set the owning side to null (unless already changed)
  204.             if ($question->getAskedBy() === $this) {
  205.                 $question->setAskedBy(null);
  206.             }
  207.         }
  208.         return $this;
  209.     }
  210.     /**
  211.      * @return Collection|Answer[]
  212.      */
  213.     public function getAnswers(): Collection
  214.     {
  215.         return $this->answers;
  216.     }
  217.     public function addAnswer(Answer $answer): self
  218.     {
  219.         if (!$this->answers->contains($answer)) {
  220.             $this->answers[] = $answer;
  221.             $answer->setAnsweredBy($this);
  222.         }
  223.         return $this;
  224.     }
  225.     public function removeAnswer(Answer $answer): self
  226.     {
  227.         if ($this->answers->removeElement($answer)) {
  228.             // set the owning side to null (unless already changed)
  229.             if ($answer->getAnsweredBy() === $this) {
  230.                 $answer->setAnsweredBy(null);
  231.             }
  232.         }
  233.         return $this;
  234.     }
  235.     /**
  236.      * @return Collection<int, Comment>
  237.      */
  238.     public function getComments(): Collection
  239.     {
  240.         return $this->comments;
  241.     }
  242.     public function addComment(Comment $comment): static
  243.     {
  244.         if (!$this->comments->contains($comment)) {
  245.             $this->comments->add($comment);
  246.             $comment->setUser($this);
  247.         }
  248.         return $this;
  249.     }
  250.     public function removeComment(Comment $comment): static
  251.     {
  252.         if ($this->comments->removeElement($comment)) {
  253.             // set the owning side to null (unless already changed)
  254.             if ($comment->getUser() === $this) {
  255.                 $comment->setUser(null);
  256.             }
  257.         }
  258.         return $this;
  259.     }
  260. }