<?php
namespace App\Entity\Saloon\Comment;
use App\Entity\Account\Customer;
use App\Entity\Saloon\Saloon;
use App\Entity\System\IpAddress;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class CommentByCustomer extends Comment
{
const MARKS = [1, 2, 3, 4, 5];
#[ORM\Column(name: 'mark', type: 'smallint', options: ['default' => 0])]
protected int $mark = 0;
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'parent', cascade: ['all'], orphanRemoval: true)]
#[ORM\OrderBy(['createdAt' => 'ASC'])]
protected Collection $comments;
public function __construct(Saloon $saloon, ?CommentByCustomer $parent, ?Customer $user, string $text, int $mark, \DateTimeImmutable $createdAt, ?IpAddress $ipAddress = null)
{
parent::__construct($saloon, $parent, $user, $text, $createdAt);
$this->mark = $mark;
$this->comments = new ArrayCollection();
if($ipAddress)
$this->ipAddress = $ipAddress;
}
public function getMark(): int
{
return $this->mark;
}
public function setMark(int $mark): void
{
$this->mark = $mark;
}
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment)
{
return $this->comments->add($comment);
}
public function removeComment(Comment $comment)
{
return $this->comments->removeElement($comment);
}
public function isCommentedByAdvertiser(): bool
{
return $this->comments->filter(function(Comment $comment): bool {
return $comment instanceof CommentByAdvertiser;
})->count() > 0;
}
public function isCommentedBySupport(): bool
{
return $this->comments->filter(function(Comment $comment): bool {
return $comment instanceof CommentBySupport;
})->count() > 0;
}
public function getLastCommentByAdvertiser(): ?CommentByAdvertiser
{
$replies = $this->comments->filter(function(Comment $comment): bool {
return $comment instanceof CommentByAdvertiser;
});
return $replies->count() > 0 ? $replies->last() : null;
}
public function getLastCommentBySupport(): ?CommentBySupport
{
$replies = $this->comments->filter(function(Comment $comment): bool {
return $comment instanceof CommentBySupport;
});
return $replies->count() > 0 ? $replies->last() : null;
}
}