src/Aqarmap/Bundle/DiscussionBundle/Entity/Comment.php line 27

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\DiscussionBundle\Entity;
  3. use Aqarmap\Bundle\DiscussionBundle\Constant\CommentStatus;
  4. use Aqarmap\Bundle\UserBundle\Entity\User;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Criteria;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use JMS\Serializer\Annotation as Serializer;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * Comment.
  13.  *
  14.  * @ORM\Entity(repositoryClass="Aqarmap\Bundle\DiscussionBundle\Repository\CommentRepository")
  15.  *
  16.  * @ORM\Table(name="comments")
  17.  *
  18.  * @ORM\HasLifecycleCallbacks
  19.  *
  20.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  21.  *
  22.  * @Serializer\ExclusionPolicy("all")
  23.  */
  24. class Comment
  25. {
  26.     /**
  27.      * @var int
  28.      *
  29.      * @ORM\Column(name="id", type="integer")
  30.      *
  31.      * @ORM\Id
  32.      *
  33.      * @ORM\GeneratedValue(strategy="AUTO")
  34.      */
  35.     private $id;
  36.     /**
  37.      * @var string
  38.      *
  39.      * @ORM\Column(name="content", type="text", length=512, nullable=true)
  40.      *
  41.      * @Assert\NotBlank()
  42.      *
  43.      * @Serializer\Groups({"Comment"})
  44.      *
  45.      * @Serializer\Expose
  46.      */
  47.     private $content;
  48.     /**
  49.      * @var \DateTime
  50.      *
  51.      * @Serializer\Groups({"Comment"})
  52.      *
  53.      * @Serializer\Expose
  54.      *
  55.      * @ORM\Column(name="created_at", type="datetime", nullable=true)
  56.      */
  57.     private $createdAt;
  58.     /**
  59.      * @var \DateTime
  60.      *
  61.      * @Serializer\Groups({"Comment"})
  62.      *
  63.      * @Serializer\Expose
  64.      *
  65.      * @ORM\Column(name="updated_at", type="datetime", nullable=true)
  66.      */
  67.     private $updatedAt;
  68.     /**
  69.      * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
  70.      */
  71.     private $deletedAt;
  72.     /**
  73.      * @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\UserBundle\Entity\User", inversedBy="comments")
  74.      *
  75.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  76.      *
  77.      * @Serializer\Groups({"Comment"})
  78.      *
  79.      * @Serializer\Expose
  80.      */
  81.     protected $user;
  82.     /**
  83.      * @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\DiscussionBundle\Entity\Discussion", inversedBy="comments")
  84.      *
  85.      * @ORM\JoinColumn(name="discussion_id", referencedColumnName="id")
  86.      */
  87.     protected $discussion;
  88.     /**
  89.      * @ORM\OneToMany(targetEntity="CommentLike",
  90.      * mappedBy="comment", fetch="EXTRA_LAZY")
  91.      *
  92.      * @Serializer\Groups({"Comment"})
  93.      *
  94.      * @Serializer\Expose
  95.      */
  96.     protected $likes;
  97.     /**
  98.      * @var int
  99.      *
  100.      * @Serializer\Groups({"Comment"})
  101.      *
  102.      * @Serializer\Expose
  103.      *
  104.      * @ORM\Column(name="status", type="smallint", nullable=true, options={"default" = 1})
  105.      */
  106.     private $status CommentStatus::PENDING;
  107.     public function __construct()
  108.     {
  109.         $this->likes = new ArrayCollection();
  110.     }
  111.     /**
  112.      * Get id.
  113.      *
  114.      * @return int
  115.      */
  116.     public function getId()
  117.     {
  118.         return $this->id;
  119.     }
  120.     /**
  121.      * @ORM\PrePersist
  122.      */
  123.     public function onPrePersist(): void
  124.     {
  125.         if (!$this->getCreatedAt()) {
  126.             $this->setCreatedAt(new \DateTime());
  127.         } else {
  128.             $this->setUpdatedAt(new \DateTime());
  129.         }
  130.     }
  131.     /**
  132.      * Set createdAt.
  133.      *
  134.      * @param \DateTime $createdAt
  135.      *
  136.      * @return Comment
  137.      */
  138.     public function setCreatedAt($createdAt)
  139.     {
  140.         $this->createdAt $createdAt;
  141.         return $this;
  142.     }
  143.     /**
  144.      * Get createdAt.
  145.      *
  146.      * @return \DateTime
  147.      */
  148.     public function getCreatedAt()
  149.     {
  150.         return $this->createdAt;
  151.     }
  152.     /**
  153.      * Set user.
  154.      *
  155.      * @return Comment
  156.      */
  157.     public function setUser(?User $user null)
  158.     {
  159.         $this->user $user;
  160.         return $this;
  161.     }
  162.     /**
  163.      * Get user.
  164.      *
  165.      * @return User
  166.      */
  167.     public function getUser()
  168.     {
  169.         return $this->user;
  170.     }
  171.     /**
  172.      * Set content.
  173.      *
  174.      * @param string $content
  175.      *
  176.      * @return Comment
  177.      */
  178.     public function setContent($content)
  179.     {
  180.         $this->content $content;
  181.         return $this;
  182.     }
  183.     /**
  184.      * Get content.
  185.      *
  186.      * @return string
  187.      */
  188.     public function getContent()
  189.     {
  190.         return $this->content;
  191.     }
  192.     /**
  193.      * Set discussion.
  194.      *
  195.      * @return Comment
  196.      */
  197.     public function setDiscussion(?Discussion $discussion null)
  198.     {
  199.         $this->discussion $discussion;
  200.         return $this;
  201.     }
  202.     /**
  203.      * Get discussion.
  204.      *
  205.      * @return Discussion
  206.      */
  207.     public function getDiscussion()
  208.     {
  209.         return $this->discussion;
  210.     }
  211.     /**
  212.      * Set status.
  213.      *
  214.      * @param int $status
  215.      *
  216.      * @return Comment
  217.      */
  218.     public function setStatus($status)
  219.     {
  220.         $this->status $status;
  221.         return $this;
  222.     }
  223.     /**
  224.      * Get status.
  225.      *
  226.      * @return int
  227.      */
  228.     public function getStatus()
  229.     {
  230.         return $this->status;
  231.     }
  232.     public function getStatusLabel()
  233.     {
  234.         return CommentStatus::getLabel($this->getStatus());
  235.     }
  236.     public function isUserLike($user$commentType null)
  237.     {
  238.         $criteria Criteria::create()
  239.             ->where(Criteria::expr()->eq('user'$user))
  240.             ->andWhere(Criteria::expr()->eq('type'$commentType))
  241.             ->andWhere(Criteria::expr()->eq('comment'$this))
  242.         ;
  243.         return $this->likes->matching($criteria)->first() ? true false;
  244.     }
  245.     /**
  246.      * @param null $commentType
  247.      */
  248.     public function getLikes($commentType null)
  249.     {
  250.         if ($commentType) {
  251.             $criteria Criteria::create()
  252.                 ->andWhere(Criteria::expr()->eq('type'$commentType))
  253.                 ->andWhere(Criteria::expr()->eq('comment'$this))
  254.             ;
  255.             return $this->likes->matching($criteria);
  256.         }
  257.         return $this->likes;
  258.     }
  259.     public function setLikes($likes): void
  260.     {
  261.         $this->likes $likes;
  262.     }
  263.     public function getUpdatedAt()
  264.     {
  265.         return $this->updatedAt;
  266.     }
  267.     public function setUpdatedAt($updatedAt): void
  268.     {
  269.         $this->updatedAt $updatedAt;
  270.     }
  271.     public function getDeletedAt()
  272.     {
  273.         return $this->deletedAt;
  274.     }
  275.     public function setDeletedAt($deletedAt): void
  276.     {
  277.         $this->deletedAt $deletedAt;
  278.     }
  279. }