src/Entity/Comment.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\CommentRepository")
  8.  *
  9.  * @ORM\HasLifecycleCallbacks()
  10.  */
  11. class Comment
  12. {
  13.     /**
  14.      * @ORM\Id()
  15.      *
  16.      * @ORM\GeneratedValue()
  17.      *
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="articlesViewed")
  23.      *
  24.      * @ORM\JoinColumn(nullable=true)
  25.      */
  26.     private $user;
  27.     /**
  28.      * @ORM\Column(type="string", length=255, nullable=true)
  29.      */
  30.     private $author_name;
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=true)
  33.      */
  34.     private $author_email;
  35.     /**
  36.      * @ORM\Column(type="text")
  37.      */
  38.     private $content;
  39.     /**
  40.      * @ORM\ManyToOne(targetEntity="App\Entity\Article", inversedBy="comments")
  41.      *
  42.      * @ORM\JoinColumn(nullable=false)
  43.      */
  44.     private $article;
  45.     /**
  46.      * @ORM\ManyToOne(targetEntity="App\Entity\Comment", inversedBy="answers")
  47.      */
  48.     private $parent;
  49.     /**
  50.      * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="parent")
  51.      */
  52.     private $answers;
  53.     /**
  54.      * @ORM\OneToMany(targetEntity="App\Entity\UserLikes", mappedBy="comment", cascade={"persist", "remove"})
  55.      */
  56.     private $commentLikes;
  57.     public function __construct()
  58.     {
  59.         $this->answers = new ArrayCollection();
  60.         $this->commentLikes = new ArrayCollection();
  61.     }
  62.     public function getId(): ?int
  63.     {
  64.         return $this->id;
  65.     }
  66.     /**
  67.      * @return mixed
  68.      */
  69.     public function getUser()
  70.     {
  71.         return $this->user;
  72.     }
  73.     /**
  74.      * @param mixed $user
  75.      *
  76.      * @return Comment
  77.      */
  78.     public function setUser($user)
  79.     {
  80.         $this->user $user;
  81.         return $this;
  82.     }
  83.     public function getAuthorName(): ?string
  84.     {
  85.         return $this->author_name;
  86.     }
  87.     public function setAuthorName(string $author_name): self
  88.     {
  89.         $this->author_name $author_name;
  90.         return $this;
  91.     }
  92.     public function getAuthorEmail(): ?string
  93.     {
  94.         return $this->author_email;
  95.     }
  96.     public function setAuthorEmail(string $author_email): self
  97.     {
  98.         $this->author_email $author_email;
  99.         return $this;
  100.     }
  101.     public function getContent(): ?string
  102.     {
  103.         return $this->content;
  104.     }
  105.     public function setContent(string $content): self
  106.     {
  107.         $this->content $content;
  108.         return $this;
  109.     }
  110.     public function getArticle(): ?Article
  111.     {
  112.         return $this->article;
  113.     }
  114.     public function setArticle(?Article $article): self
  115.     {
  116.         $this->article $article;
  117.         return $this;
  118.     }
  119.     public function getParent(): ?self
  120.     {
  121.         return $this->parent;
  122.     }
  123.     public function setParent(?self $parent): self
  124.     {
  125.         $this->parent $parent;
  126.         return $this;
  127.     }
  128.     /**
  129.      * @return Collection|self[]
  130.      */
  131.     public function getAnswers($include_deleted false): Collection
  132.     {
  133.         if (!$include_deleted) {
  134.             $answers array_filter($this->answers->toArray(), function (self $comment) {
  135.                 return null === $comment->getDeletedAt();
  136.             });
  137.             usort($answers, function (self $aself $b) {
  138.                 return $a->getId() - $b->getId();
  139.             });
  140.             return new ArrayCollection($answers);
  141.         }
  142.         return $this->answers;
  143.     }
  144.     public function addAnswer(self $answer): self
  145.     {
  146.         if (!$this->answers->contains($answer)) {
  147.             $this->answers[] = $answer;
  148.             $answer->setParent($this);
  149.         }
  150.         return $this;
  151.     }
  152.     public function removeAnswer(self $answer): self
  153.     {
  154.         if ($this->answers->contains($answer)) {
  155.             $this->answers->removeElement($answer);
  156.             // set the owning side to null (unless already changed)
  157.             if ($answer->getParent() === $this) {
  158.                 $answer->setParent(null);
  159.             }
  160.         }
  161.         return $this;
  162.     }
  163.     /**
  164.      * @ORM\Column(type="datetime", nullable=false)
  165.      */
  166.     private $created_at;
  167.     /**
  168.      * @ORM\Column(type="datetime", nullable=true)
  169.      */
  170.     private $updated_at null;
  171.     public function getCreatedAt(): \DateTimeInterface
  172.     {
  173.         return $this->created_at;
  174.     }
  175.     public function setCreatedAt(\DateTimeInterface $created_at): self
  176.     {
  177.         $this->created_at $created_at;
  178.         return $this;
  179.     }
  180.     public function getUpdatedAt(): ?\DateTimeInterface
  181.     {
  182.         return $this->updated_at;
  183.     }
  184.     public function setUpdatedAt(?\DateTimeInterface $updated_at): self
  185.     {
  186.         $this->updated_at $updated_at;
  187.         return $this;
  188.     }
  189.     /**
  190.      * @ORM\PrePersist
  191.      */
  192.     public function setCreatedAtValue()
  193.     {
  194.         $this->setCreatedAt(new \DateTime());
  195.     }
  196.     /**
  197.      * @ORM\PreUpdate
  198.      */
  199.     public function setUpdatedAtValue()
  200.     {
  201.         $this->setUpdatedAt(new \DateTime());
  202.     }
  203.     /**
  204.      * @ORM\Column(type="datetime", nullable=true)
  205.      */
  206.     private $deleted_at;
  207.     public function getDeletedAt(): ?\DateTimeInterface
  208.     {
  209.         return $this->deleted_at;
  210.     }
  211.     public function setDeletedAt(?\DateTimeInterface $deleted_at): self
  212.     {
  213.         $this->deleted_at $deleted_at;
  214.         return $this;
  215.     }
  216.     /**
  217.      * @return ArrayCollection
  218.      */
  219.     public function getCommentLikes(): Collection
  220.     {
  221.         return $this->commentLikes;
  222.     }
  223.     public function addCommentLikes(UserLikes $like): self
  224.     {
  225.         if (!$this->commentLikes->contains($like)) {
  226.             $this->commentLikes[] = $like;
  227.             $like->setComment($this);
  228.         }
  229.         return $this;
  230.     }
  231.     public function removeCommentLikes(UserLikes $like): self
  232.     {
  233.         if ($this->commentLikes->contains($like)) {
  234.             $this->commentLikes->removeElement($like);
  235.             // set the owning side to null (unless already changed)
  236.             if ($like->getComment() === $this) {
  237.                 $like->setComment(null);
  238.             }
  239.         }
  240.         return $this;
  241.     }
  242.     public function isLikedByUser(User $user): bool
  243.     {
  244.         foreach ($this->commentLikes as $like) {
  245.             if ($like->getUser() === $user) {
  246.                 return true;
  247.             }
  248.         }
  249.         return false;
  250.     }
  251. }