<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CommentRepository")
*
* @ORM\HasLifecycleCallbacks()
*/
class Comment
{
/**
* @ORM\Id()
*
* @ORM\GeneratedValue()
*
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="articlesViewed")
*
* @ORM\JoinColumn(nullable=true)
*/
private $user;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $author_name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $author_email;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Article", inversedBy="comments")
*
* @ORM\JoinColumn(nullable=false)
*/
private $article;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Comment", inversedBy="answers")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="parent")
*/
private $answers;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserLikes", mappedBy="comment", cascade={"persist", "remove"})
*/
private $commentLikes;
public function __construct()
{
$this->answers = new ArrayCollection();
$this->commentLikes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @param mixed $user
*
* @return Comment
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
public function getAuthorName(): ?string
{
return $this->author_name;
}
public function setAuthorName(string $author_name): self
{
$this->author_name = $author_name;
return $this;
}
public function getAuthorEmail(): ?string
{
return $this->author_email;
}
public function setAuthorEmail(string $author_email): self
{
$this->author_email = $author_email;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getArticle(): ?Article
{
return $this->article;
}
public function setArticle(?Article $article): self
{
$this->article = $article;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection|self[]
*/
public function getAnswers($include_deleted = false): Collection
{
if (!$include_deleted) {
$answers = array_filter($this->answers->toArray(), function (self $comment) {
return null === $comment->getDeletedAt();
});
usort($answers, function (self $a, self $b) {
return $a->getId() - $b->getId();
});
return new ArrayCollection($answers);
}
return $this->answers;
}
public function addAnswer(self $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setParent($this);
}
return $this;
}
public function removeAnswer(self $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
// set the owning side to null (unless already changed)
if ($answer->getParent() === $this) {
$answer->setParent(null);
}
}
return $this;
}
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $created_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updated_at = null;
public function getCreatedAt(): \DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updated_at;
}
public function setUpdatedAt(?\DateTimeInterface $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
/**
* @ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->setCreatedAt(new \DateTime());
}
/**
* @ORM\PreUpdate
*/
public function setUpdatedAtValue()
{
$this->setUpdatedAt(new \DateTime());
}
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $deleted_at;
public function getDeletedAt(): ?\DateTimeInterface
{
return $this->deleted_at;
}
public function setDeletedAt(?\DateTimeInterface $deleted_at): self
{
$this->deleted_at = $deleted_at;
return $this;
}
/**
* @return ArrayCollection
*/
public function getCommentLikes(): Collection
{
return $this->commentLikes;
}
public function addCommentLikes(UserLikes $like): self
{
if (!$this->commentLikes->contains($like)) {
$this->commentLikes[] = $like;
$like->setComment($this);
}
return $this;
}
public function removeCommentLikes(UserLikes $like): self
{
if ($this->commentLikes->contains($like)) {
$this->commentLikes->removeElement($like);
// set the owning side to null (unless already changed)
if ($like->getComment() === $this) {
$like->setComment(null);
}
}
return $this;
}
public function isLikedByUser(User $user): bool
{
foreach ($this->commentLikes as $like) {
if ($like->getUser() === $user) {
return true;
}
}
return false;
}
}