<?php
namespace Aqarmap\Bundle\DiscussionBundle\Entity;
use Aqarmap\Bundle\DiscussionBundle\Constant\CommentStatus;
use Aqarmap\Bundle\UserBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Comment.
*
* @ORM\Entity(repositoryClass="Aqarmap\Bundle\DiscussionBundle\Repository\CommentRepository")
*
* @ORM\Table(name="comments")
*
* @ORM\HasLifecycleCallbacks
*
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*
* @Serializer\ExclusionPolicy("all")
*/
class Comment
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="content", type="text", length=512, nullable=true)
*
* @Assert\NotBlank()
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*/
private $content;
/**
* @var \DateTime
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
private $createdAt;
/**
* @var \DateTime
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\UserBundle\Entity\User", inversedBy="comments")
*
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="\Aqarmap\Bundle\DiscussionBundle\Entity\Discussion", inversedBy="comments")
*
* @ORM\JoinColumn(name="discussion_id", referencedColumnName="id")
*/
protected $discussion;
/**
* @ORM\OneToMany(targetEntity="CommentLike",
* mappedBy="comment", fetch="EXTRA_LAZY")
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*/
protected $likes;
/**
* @var int
*
* @Serializer\Groups({"Comment"})
*
* @Serializer\Expose
*
* @ORM\Column(name="status", type="smallint", nullable=true, options={"default" = 1})
*/
private $status = CommentStatus::PENDING;
public function __construct()
{
$this->likes = new ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @ORM\PrePersist
*/
public function onPrePersist(): void
{
if (!$this->getCreatedAt()) {
$this->setCreatedAt(new \DateTime());
} else {
$this->setUpdatedAt(new \DateTime());
}
}
/**
* Set createdAt.
*
* @param \DateTime $createdAt
*
* @return Comment
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt.
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set user.
*
* @return Comment
*/
public function setUser(?User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user.
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set content.
*
* @param string $content
*
* @return Comment
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content.
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set discussion.
*
* @return Comment
*/
public function setDiscussion(?Discussion $discussion = null)
{
$this->discussion = $discussion;
return $this;
}
/**
* Get discussion.
*
* @return Discussion
*/
public function getDiscussion()
{
return $this->discussion;
}
/**
* Set status.
*
* @param int $status
*
* @return Comment
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status.
*
* @return int
*/
public function getStatus()
{
return $this->status;
}
public function getStatusLabel()
{
return CommentStatus::getLabel($this->getStatus());
}
public function isUserLike($user, $commentType = null)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('user', $user))
->andWhere(Criteria::expr()->eq('type', $commentType))
->andWhere(Criteria::expr()->eq('comment', $this))
;
return $this->likes->matching($criteria)->first() ? true : false;
}
/**
* @param null $commentType
*/
public function getLikes($commentType = null)
{
if ($commentType) {
$criteria = Criteria::create()
->andWhere(Criteria::expr()->eq('type', $commentType))
->andWhere(Criteria::expr()->eq('comment', $this))
;
return $this->likes->matching($criteria);
}
return $this->likes;
}
public function setLikes($likes): void
{
$this->likes = $likes;
}
public function getUpdatedAt()
{
return $this->updatedAt;
}
public function setUpdatedAt($updatedAt): void
{
$this->updatedAt = $updatedAt;
}
public function getDeletedAt()
{
return $this->deletedAt;
}
public function setDeletedAt($deletedAt): void
{
$this->deletedAt = $deletedAt;
}
}