src/Entity/User.php line 100

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\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use App\Entity\Categorie;
  11. use App\Entity\Article;
  12. use App\Entity\Post;
  13. #[ORM\Entity(repositoryClassUserRepository::class)]
  14. #[ORM\Table(name"user")]
  15. // montre que ce NOM user est UNIQUE
  16. #[UniqueEntity(fields: ['username'], message'Il existe déjà un compte avec ce nom d\'utilisateur')]
  17. class User implements UserInterfacePasswordAuthenticatedUserInterface
  18. {
  19.     #[ORM\Id]
  20.     #[ORM\GeneratedValue]
  21.     #[ORM\Column]
  22.     private ?int $id null;
  23.     #[ORM\Column(length180uniquetrue)]
  24.     private ?string $username null;
  25.     #[ORM\Column(length255)]
  26.     private ?string $email null;
  27.     
  28.     #[ORM\Column]
  29.     private array $roles = [];
  30.     /**
  31.      * @var string 
  32.      */
  33.     #[ORM\Column]
  34.     private ?string $password null;
  35.     #[ORM\Column(type'boolean')]
  36.     private $isVerified false;
  37.     
  38.     
  39.     #[ORM\OneToMany(targetEntityMessage::class, mappedBy'expediteur'orphanRemovaltrue)]
  40. private Collection $sentMessages;
  41. #[ORM\OneToMany(targetEntityMessage::class, mappedBy'destinataire'orphanRemovaltrue)]
  42. private Collection $receivedMessages;
  43.     #[ORM\ManyToMany(targetEntityself::class)]
  44.     #[ORM\JoinTable(
  45.         name'user_friends',
  46.         joinColumns: [new ORM\JoinColumn(name'user_id'referencedColumnName'id')],
  47.         inverseJoinColumns: [new ORM\JoinColumn(name'friend_user_id'referencedColumnName'id')]
  48.     )]
  49.     private Collection $friends;
  50.     //RELATIONS Collection est une structure de donnée qui représente un groupe d'objet
  51.     #[ORM\OneToMany(targetEntityArticle::class, mappedBy'user'orphanRemovaltrue)]
  52. private Collection $articles;
  53. // Pour Posts
  54. #[ORM\OneToMany(targetEntityPost::class, mappedBy'user'orphanRemovaltrue)]
  55. private Collection $posts;
  56.     
  57.     #[ORM\ManyToMany(targetEntity:User::class, mappedBy:"receivedFriendRequests")]
  58.     private $sentFriendRequests;
  59.     
  60.     #[ORM\ManyToMany(targetEntity:User::class, inversedBy:"sentFriendRequests")]
  61.       #[ORM\JoinTable(
  62.           name:"user_friend_requests",
  63.           joinColumns: [new ORM\JoinColumn(name:"sender_id"referencedColumnName:"id")],
  64.           inverseJoinColumns: [new ORM\JoinColumn(name:"receiver_id"referencedColumnName:"id")]
  65.       )]
  66.     private Collection $receivedFriendRequests;
  67.     //initialise articles et post
  68.     public function __construct()
  69.     {
  70.         /* ArrayCollection pour manipuler facilement ces collections */
  71.         $this->articles = new ArrayCollection();
  72.         $this->posts = new ArrayCollection();
  73.         $this->sentMessages = new ArrayCollection();
  74.         $this->receivedMessages = new ArrayCollection();
  75.         $this->friends = new ArrayCollection();
  76.         $this->myFriends = new ArrayCollection();
  77.         $this->sentFriendRequests = new ArrayCollection();
  78.         $this->receivedFriendRequests = new ArrayCollection();
  79.     }
  80.     /* FRIENDS */
  81.      /**
  82.      * @return Collection|User[]
  83.      */
  84.     public function getFriends(): Collection
  85.     {
  86.         return $this->friends;
  87.     }
  88.     public function addFriend(User $friend): self
  89.     {
  90.         if (!$this->friends->contains($friend)) {
  91.             $this->friends[] = $friend;
  92.         }
  93.         return $this;
  94.     }
  95.     public function removeFriend(User $friend): self
  96.     {
  97.         $this->friends->removeElement($friend);
  98.         return $this;
  99.     }
  100.     /* REQUETE DEMANDE AMI */
  101.     public function getSentFriendRequests(): Collection
  102.     {
  103.         return $this->sentFriendRequests;
  104.     }
  105.     public function addSentFriendRequest(User $user): self
  106.     {
  107.         if (!$this->sentFriendRequests->contains($user)) {
  108.             $this->sentFriendRequests[] = $user;
  109.         }
  110.         return $this;
  111.     }
  112.     public function removeSentFriendRequest(User $user): self
  113.     {
  114.         $this->sentFriendRequests->removeElement($user);
  115.         return $this;
  116.     }
  117.     /* MESSAGE ENVOYER */
  118.      /**
  119.      * @return Collection|Message[]
  120.      */
  121.     public function getSentMessages(): Collection
  122.     {
  123.         return $this->sentMessages;
  124.     }
  125.     public function addSentMessage(Message $sentMessage): self
  126.     {
  127.         if (!$this->sentMessages->contains($sentMessage)) {
  128.             $this->sentMessages[] = $sentMessage;
  129.             $sentMessage->setExpediteur($this);
  130.         }
  131.         return $this;
  132.     }
  133.     public function removeSentMessage(Message $sentMessage): self
  134.     {
  135.         if ($this->sentMessages->removeElement($sentMessage)) {
  136.             if ($sentMessage->getExpediteur() === $this) {
  137.                 $sentMessage->setExpediteur(null);
  138.             }
  139.         }
  140.         return $this;
  141.     }
  142.     /* RECEVOIR MESSAGE */
  143.     /**
  144.      * @return Collection|Message[]
  145.      */
  146.     public function getReceivedMessages(): Collection
  147.     {
  148.         return $this->receivedMessages;
  149.     }
  150.     public function addReceivedMessage(Message $receivedMessage): self
  151.     {
  152.         if (!$this->receivedMessages->contains($receivedMessage)) {
  153.             $this->receivedMessages[] = $receivedMessage;
  154.             $receivedMessage->setDestinataire($this);
  155.         }
  156.         return $this;
  157.     }
  158.     public function removeReceivedMessage(Message $receivedMessage): self
  159.     {
  160.         if ($this->receivedMessages->removeElement($receivedMessage)) {
  161.             if ($receivedMessage->getDestinataire() === $this) {
  162.                 $receivedMessage->setDestinataire(null);
  163.             }
  164.         }
  165.         return $this;
  166.     }
  167.     public function getId(): ?int
  168.     {
  169.         return $this->id;
  170.     }
  171.    
  172.     public function getEmail(): ?string
  173.     {
  174.         return $this->email;
  175.     }
  176.     public function setEmail(string $email): static
  177.     {
  178.         $this->email $email;
  179.         return $this;
  180.     }
  181.     
  182.     public function setUsername(string $username): static
  183.     {
  184.         $this->username $username;
  185.         return $this;
  186.     }
  187.     /**
  188.      * A visual identifier that represents this user.
  189.      *
  190.      * @see UserInterface
  191.      */
  192.     public function getUsername(): string
  193.     {
  194.         return (string) $this->username;
  195.     }
  196.     /**
  197.      * @see UserInterface
  198.      */
  199.     public function getRoles(): array
  200.     {
  201.         $roles $this->roles;
  202.         return array_unique($roles);
  203.     }
  204.     public function setRoles(array $roles): static
  205.     {
  206.         $this->roles $roles;
  207.         return $this;
  208.     }
  209.     
  210.     /**
  211.      * @see PasswordAuthenticatedUserInterface
  212.      */
  213.     public function getPassword(): string
  214.     {
  215.         return $this->password;
  216.     }
  217.     public function setPassword(string $password): static
  218.     {
  219.         $this->password $password;
  220.         return $this;
  221.     }
  222.     public function getReceivedFriendRequests(): Collection
  223.     {
  224.         return $this->receivedFriendRequests;
  225.     }
  226.     public function addReceivedFriendRequest(User $user): self
  227.     {
  228.         if (!$this->receivedFriendRequests->contains($user)) {
  229.             $this->receivedFriendRequests[] = $user;
  230.         }
  231.         return $this;
  232.     }
  233.     public function removeReceivedFriendRequest(User $user): self
  234.     {
  235.         $this->receivedFriendRequests->removeElement($user);
  236.         return $this;
  237.     }
  238.     
  239.     /**
  240.      *  "salt" est un algorithme de hachage moderne pour MDP dans votre fichier security.yaml.
  241.     *
  242.      * @see UserInterface
  243.      */
  244.     public function getSalt(): ?string
  245.     {
  246.         return null;
  247.     }
  248.     /* Ajouter par symfony afin d'effacer tt donnée apres authentification */
  249.     /**
  250.      * @see UserInterface
  251.      */
  252.     public function eraseCredentials(): void
  253.     {
  254.         // If you store any temporary, sensitive data on the user, clear it here
  255.         // $this->plainPassword = null;
  256.     }
  257.     
  258.     public function isVerified(): bool
  259.     {
  260.         return $this->isVerified;
  261.     }
  262.     public function setIsVerified(bool $isVerified): static
  263.     {
  264.         $this->isVerified $isVerified;
  265.         return $this;
  266.     }
  267.     /*ARTICLE  */
  268.     /**
  269.      * @return Collection<int, Article>
  270.      */
  271.     public function getArticles(): Collection
  272.     {
  273.         return $this->articles;
  274.     }
  275.     public function addArticle(Article $article): static
  276.     {
  277.         if (!$this->articles->contains($article)) {
  278.             $this->articles->add($article);
  279.             $article->setUser($this);
  280.         }
  281.         return $this;
  282.     }
  283.     public function removeArticle(Article $article): static
  284.     {
  285.         if ($this->articles->removeElement($article)) {
  286.             if ($article->getUser() === $this) {
  287.                 $article->setUser(null);
  288.             }
  289.         }
  290.         return $this;
  291.     }
  292.     /**
  293.      * @return Collection<int, Post>
  294.      */
  295.     public function getPosts(): Collection
  296.     {
  297.         return $this->posts;
  298.     }
  299.     public function addPost(Post $post): static
  300.     {
  301.         if (!$this->posts->contains($post)) {
  302.             $this->posts->add($post);
  303.             $post->setUser($this);
  304.         }
  305.         return $this;
  306.     }
  307.     public function removePost(Post $post): static
  308.     {
  309.         if ($this->posts->removeElement($post)) {
  310.             if ($post->getUser() === $this) {
  311.                 $post->setUser(null);
  312.             }
  313.         }
  314.         return $this;
  315.     }
  316. }