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\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * @ORM\Entity(repositoryClass=UserRepository::class)
  11.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  12.  */
  13. class User implements UserInterface
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=180, unique=true)
  23.      */
  24.     private $email;
  25.     /**
  26.      * @ORM\Column(type="json")
  27.      */
  28.     private $roles = [];
  29.     /**
  30.      * @var string The hashed password
  31.      * @ORM\Column(type="string")
  32.      */
  33.     private $password;
  34.     /**
  35.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="Author")
  36.      */
  37.     private $comments;
  38.     /**
  39.      * @ORM\Column(type="string", length=255)
  40.      */
  41.     private $name;
  42.     public function __construct()
  43.     {
  44.         $this->comments = new ArrayCollection();
  45.     }
  46.     public function getId(): ?int
  47.     {
  48.         return $this->id;
  49.     }
  50.     public function getEmail(): ?string
  51.     {
  52.         return $this->email;
  53.     }
  54.     public function setEmail(string $email): self
  55.     {
  56.         $this->email $email;
  57.         return $this;
  58.     }
  59.     /**
  60.      * A visual identifier that represents this user.
  61.      *
  62.      * @see UserInterface
  63.      */
  64.     public function getUsername(): string
  65.     {
  66.         return (string) $this->email;
  67.     }
  68.     /**
  69.      * @see UserInterface
  70.      */
  71.     public function getRoles(): array
  72.     {
  73.         $roles $this->roles;
  74.         // guarantee every user at least has ROLE_USER
  75.         $roles[] = 'ROLE_USER';
  76.         return array_unique($roles);
  77.     }
  78.     public function setRoles(array $roles): self
  79.     {
  80.         $this->roles $roles;
  81.         return $this;
  82.     }
  83.     /**
  84.      * @see UserInterface
  85.      */
  86.     public function getPassword(): string
  87.     {
  88.         return (string) $this->password;
  89.     }
  90.     public function setPassword(string $password): self
  91.     {
  92.         $this->password $password;
  93.         return $this;
  94.     }
  95.     /**
  96.      * Returning a salt is only needed, if you are not using a modern
  97.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  98.      *
  99.      * @see UserInterface
  100.      */
  101.     public function getSalt(): ?string
  102.     {
  103.         return null;
  104.     }
  105.     /**
  106.      * @see UserInterface
  107.      */
  108.     public function eraseCredentials()
  109.     {
  110.         // If you store any temporary, sensitive data on the user, clear it here
  111.         // $this->plainPassword = null;
  112.     }
  113.     /**
  114.      * @return Collection|Comment[]
  115.      */
  116.     public function getComments(): Collection
  117.     {
  118.         return $this->comments;
  119.     }
  120.     public function addComment(Comment $comment): self
  121.     {
  122.         if (!$this->comments->contains($comment)) {
  123.             $this->comments[] = $comment;
  124.             $comment->setAuthor($this);
  125.         }
  126.         return $this;
  127.     }
  128.     public function removeComment(Comment $comment): self
  129.     {
  130.         if ($this->comments->removeElement($comment)) {
  131.             // set the owning side to null (unless already changed)
  132.             if ($comment->getAuthor() === $this) {
  133.                 $comment->setAuthor(null);
  134.             }
  135.         }
  136.         return $this;
  137.     }
  138.     public function getName(): ?string
  139.     {
  140.         return $this->name;
  141.     }
  142.     public function setName(string $name): self
  143.     {
  144.         $this->name $name;
  145.         return $this;
  146.     }
  147. }