src/Entity/User.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Repository\UserRepository;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Doctrine\Common\Collections\Collection;
  7. use ApiPlatform\Core\Annotation\ApiResource;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. /**
  14.  * @ORM\Entity(repositoryClass=UserRepository::class)
  15.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  16.  * @ApiResource(
  17.  *      normalizationContext={"groups" = {"user_read"}}
  18.  * )
  19.  */
  20. class User implements UserInterfacePasswordAuthenticatedUserInterface
  21. {
  22.     /**
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue
  25.      * @ORM\Column(type="integer")
  26.      * @Groups({"user_read", "news_read"})
  27.      */
  28.     private $id;
  29.     /**
  30.      * @ORM\Column(type="string", length=50, unique=true)
  31.      * @Groups({"user_read", "news_read"})
  32.      */
  33.     private $email;
  34.     /**
  35.      * @ORM\Column(type="json")
  36.      * @Groups({"user_read", "news_read"})
  37.      */
  38.     private $roles = [];
  39.     /**
  40.      * @var string The hashed password
  41.      * @ORM\Column(type="string")
  42.      */
  43.     private $password;
  44.     /**
  45.      * @ORM\Column(type="string", length=50)
  46.      * @Groups({"user_read", "news_read"})
  47.      */
  48.     private $firstName;
  49.     /**
  50.      * @ORM\Column(type="string", length=50)
  51.      * @Groups({"user_read", "news_read"})
  52.      */
  53.     private $lastName;
  54.     /**
  55.      * @ORM\Column(type="boolean")
  56.      * @Groups({"user_read", "news_read"})
  57.      */
  58.     private $agreeTerms false;
  59.     /**
  60.      * @var \DateTime $createdAt
  61.      * 
  62.      * @Gedmo\Timestampable(on="create")
  63.      * @ORM\Column(type="datetime")
  64.      * @Groups({"user_read", "news_read"})
  65.      */
  66.     private $createdAt;
  67.     /**
  68.      * @var \DateTime $updatedAt
  69.      *
  70.      * @Gedmo\Timestampable(on="update")
  71.      * @ORM\Column(type="datetime")
  72.      * @Groups({"user_read", "news_read"})
  73.      */
  74.     private $updatedAt;
  75.     /**
  76.      * @ORM\OneToMany(targetEntity=News::class, mappedBy="author", orphanRemoval=true)
  77.      * @Groups({"user_read"})
  78.      */
  79.     private $news;
  80.     /**
  81.      * @ORM\Column(type="boolean")
  82.      * @Groups({"user_read", "news_read"})
  83.      */
  84.     private $isVerified false;
  85.     public function __construct()
  86.     {
  87.         $this->news = new ArrayCollection();
  88.     }
  89.     public function __toString()
  90.     {
  91.         return $this->getEmail();
  92.     }
  93.     public function getId(): ?int
  94.     {
  95.         return $this->id;
  96.     }
  97.     public function getEmail(): ?string
  98.     {
  99.         return $this->email;
  100.     }
  101.     public function setEmail(string $email): self
  102.     {
  103.         $this->email $email;
  104.         return $this;
  105.     }
  106.     /**
  107.      * A visual identifier that represents this user.
  108.      *
  109.      * @see UserInterface
  110.      */
  111.     public function getUserIdentifier(): string
  112.     {
  113.         return (string) $this->email;
  114.     }
  115.     /**
  116.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  117.      */
  118.     public function getUsername(): string
  119.     {
  120.         return (string) $this->email;
  121.     }
  122.     /**
  123.      * @see UserInterface
  124.      */
  125.     public function getRoles(): array
  126.     {
  127.         $roles $this->roles;
  128.         // guarantee every user at least has ROLE_USER
  129.         $roles[] = 'ROLE_USER';
  130.         return array_unique($roles);
  131.     }
  132.     public function setRoles(array $roles): self
  133.     {
  134.         $this->roles $roles;
  135.         return $this;
  136.     }
  137.     /**
  138.      * @see PasswordAuthenticatedUserInterface
  139.      */
  140.     public function getPassword(): string
  141.     {
  142.         return $this->password;
  143.     }
  144.     public function setPassword(string $password): self
  145.     {
  146.         $this->password $password;
  147.         return $this;
  148.     }
  149.     /**
  150.      * Returning a salt is only needed, if you are not using a modern
  151.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  152.      *
  153.      * @see UserInterface
  154.      */
  155.     public function getSalt(): ?string
  156.     {
  157.         return null;
  158.     }
  159.     /**
  160.      * @see UserInterface
  161.      */
  162.     public function eraseCredentials()
  163.     {
  164.         // If you store any temporary, sensitive data on the user, clear it here
  165.         // $this->plainPassword = null;
  166.     }
  167.     public function getFirstName(): ?string
  168.     {
  169.         return $this->firstName;
  170.     }
  171.     public function setFirstName(string $firstName): self
  172.     {
  173.         $this->firstName $firstName;
  174.         return $this;
  175.     }
  176.     public function getLastName(): ?string
  177.     {
  178.         return $this->lastName;
  179.     }
  180.     public function setLastName(string $lastName): self
  181.     {
  182.         $this->lastName $lastName;
  183.         return $this;
  184.     }
  185.     public function getAgreeTerms(): ?bool
  186.     {
  187.         return $this->agreeTerms;
  188.     }
  189.     public function setAgreeTerms(bool $agreeTerms): self
  190.     {
  191.         $this->agreeTerms $agreeTerms;
  192.         return $this;
  193.     }
  194.     public function getCreatedAt(): ?\DateTimeInterface
  195.     {
  196.         return $this->createdAt;
  197.     }
  198.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  199.     {
  200.         $this->createdAt $createdAt;
  201.         return $this;
  202.     }
  203.     public function getUpdatedAt(): ?\DateTimeInterface
  204.     {
  205.         return $this->updatedAt;
  206.     }
  207.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  208.     {
  209.         $this->updatedAt $updatedAt;
  210.         return $this;
  211.     }
  212.     /**
  213.      * @return Collection|News[]
  214.      */
  215.     public function getNews(): Collection
  216.     {
  217.         return $this->news;
  218.     }
  219.     public function addNews(News $news): self
  220.     {
  221.         if (!$this->news->contains($news)) {
  222.             $this->news[] = $news;
  223.             $news->setAuthor($this);
  224.         }
  225.         return $this;
  226.     }
  227.     public function removeNews(News $news): self
  228.     {
  229.         if ($this->news->removeElement($news)) {
  230.             // set the owning side to null (unless already changed)
  231.             if ($news->getAuthor() === $this) {
  232.                 $news->setAuthor(null);
  233.             }
  234.         }
  235.         return $this;
  236.     }
  237.     public function isVerified(): bool
  238.     {
  239.         return $this->isVerified;
  240.     }
  241.     public function setIsVerified(bool $isVerified): self
  242.     {
  243.         $this->isVerified $isVerified;
  244.         return $this;
  245.     }
  246. }