src/Entity/User.php line 16

Open in your IDE?
  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.     public function __construct()
  46.     {
  47.         $this->questions = new ArrayCollection();
  48.         $this->answers = new ArrayCollection();
  49.     }
  50.     public function __toString(): string
  51.     {
  52.         return $this->getFullName();
  53.     }
  54.     public function getId(): ?int
  55.     {
  56.         return $this->id;
  57.     }
  58.     public function getEmail(): ?string
  59.     {
  60.         return $this->email;
  61.     }
  62.     public function setEmail(string $email): self
  63.     {
  64.         $this->email $email;
  65.         return $this;
  66.     }
  67.     /**
  68.      * A visual identifier that represents this user.
  69.      *
  70.      * @see UserInterface
  71.      */
  72.     public function getUserIdentifier(): string
  73.     {
  74.         return (string) $this->email;
  75.     }
  76.     /**
  77.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  78.      */
  79.     public function getUsername(): string
  80.     {
  81.         return (string) $this->email;
  82.     }
  83.     /**
  84.      * @see UserInterface
  85.      */
  86.     public function getRoles(): array
  87.     {
  88.         $roles $this->roles;
  89.         // guarantee every user at least has ROLE_USER
  90.         $roles[] = 'ROLE_USER';
  91.         return array_unique($roles);
  92.     }
  93.     public function setRoles(array $roles): self
  94.     {
  95.         $this->roles $roles;
  96.         return $this;
  97.     }
  98.     /**
  99.      * @see PasswordAuthenticatedUserInterface
  100.      */
  101.     public function getPassword(): string
  102.     {
  103.         return $this->password;
  104.     }
  105.     public function setPassword(string $password): self
  106.     {
  107.         $this->password $password;
  108.         return $this;
  109.     }
  110.     public function getPlainPassword(): string
  111.     {
  112.         return $this->plainPassword;
  113.     }
  114.     public function setPlainPassword(string $plainPassword): void
  115.     {
  116.         $this->plainPassword $plainPassword;
  117.     }
  118.     /**
  119.      * Returning a salt is only needed, if you are not using a modern
  120.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  121.      *
  122.      * @see UserInterface
  123.      */
  124.     public function getSalt(): ?string
  125.     {
  126.         return null;
  127.     }
  128.     /**
  129.      * @see UserInterface
  130.      */
  131.     public function eraseCredentials()
  132.     {
  133.         // If you store any temporary, sensitive data on the user, clear it here
  134.          $this->plainPassword null;
  135.     }
  136.     public function isEnabled(): bool
  137.     {
  138.         return $this->enabled;
  139.     }
  140.     public function setEnabled(bool $enabled): void
  141.     {
  142.         $this->enabled $enabled;
  143.     }
  144.     public function getFirstName(): ?string
  145.     {
  146.         return $this->firstName;
  147.     }
  148.     public function setFirstName(string $firstName): void
  149.     {
  150.         $this->firstName $firstName;
  151.     }
  152.     public function getLastName(): ?string
  153.     {
  154.         return $this->lastName;
  155.     }
  156.     public function setLastName(string $lastName): void
  157.     {
  158.         $this->lastName $lastName;
  159.     }
  160.     public function getFullName(): ?string
  161.     {
  162.         return $this->firstName.' '.$this->lastName;
  163.     }
  164.     public function getAvatar(): ?string
  165.     {
  166.         return $this->avatar;
  167.     }
  168.     public function getAvatarUrl(): ?string
  169.     {
  170.         if (!$this->avatar) {
  171.             return null;
  172.         }
  173.         if (strpos($this->avatar'/') !== false) {
  174.             return $this->avatar;
  175.         }
  176.         return sprintf('/uploads/avatars/%s'$this->avatar);
  177.     }
  178.     public function setAvatar(?string $avatar): void
  179.     {
  180.         $this->avatar $avatar;
  181.     }
  182.     /**
  183.      * @return Collection|Question[]
  184.      */
  185.     public function getQuestions(): Collection
  186.     {
  187.         return $this->questions;
  188.     }
  189.     public function addQuestion(Question $question): self
  190.     {
  191.         if (!$this->questions->contains($question)) {
  192.             $this->questions[] = $question;
  193.             $question->setAskedBy($this);
  194.         }
  195.         return $this;
  196.     }
  197.     public function removeQuestion(Question $question): self
  198.     {
  199.         if ($this->questions->removeElement($question)) {
  200.             // set the owning side to null (unless already changed)
  201.             if ($question->getAskedBy() === $this) {
  202.                 $question->setAskedBy(null);
  203.             }
  204.         }
  205.         return $this;
  206.     }
  207.     /**
  208.      * @return Collection|Answer[]
  209.      */
  210.     public function getAnswers(): Collection
  211.     {
  212.         return $this->answers;
  213.     }
  214.     public function addAnswer(Answer $answer): self
  215.     {
  216.         if (!$this->answers->contains($answer)) {
  217.             $this->answers[] = $answer;
  218.             $answer->setAnsweredBy($this);
  219.         }
  220.         return $this;
  221.     }
  222.     public function removeAnswer(Answer $answer): self
  223.     {
  224.         if ($this->answers->removeElement($answer)) {
  225.             // set the owning side to null (unless already changed)
  226.             if ($answer->getAnsweredBy() === $this) {
  227.                 $answer->setAnsweredBy(null);
  228.             }
  229.         }
  230.         return $this;
  231.     }
  232. }