<?php
namespace App\Entity\Saloon\Comment;
use App\Entity\Saloon\Saloon;
use App\Entity\System\IpAddress;
use App\Entity\User;
use App\Repository\SaloonCommentRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Table(name: 'saloon_comments')]
#[ORM\Entity(repositoryClass: SaloonCommentRepository::class)]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'account_type', type: 'string', length: 12)]
#[ORM\DiscriminatorMap(['customer' => CommentByCustomer::class, 'advertiser' => CommentByAdvertiser::class, 'support' => CommentBySupport::class])]
abstract class Comment
{
use SoftDeleteableEntity;
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected int $id;
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', nullable: true)]
#[ORM\ManyToOne(targetEntity: Comment::class)]
protected ?Comment $parent;
//, inversedBy="comments"
#[ORM\JoinColumn(name: 'saloon_id', referencedColumnName: 'id', nullable: true)]
#[ORM\ManyToOne(targetEntity: Saloon::class, inversedBy: 'comments')]
protected ?Saloon $saloon;
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true)]
#[ORM\ManyToOne(targetEntity: User::class)]
#[Groups(['preview', 'comments'])]
protected ?User $user;
#[ORM\Column(name: 'text', type: 'text')]
#[Groups(['preview', 'comments'])]
protected string $text;
#[ORM\Column(name: 'author', type: 'string', length: 64, nullable: true)]
#[Groups(['preview', 'comments'])]
protected ?string $author;
#[ORM\Column(name: 'created_at', type: 'datetimetz_immutable')]
#[Groups(['preview', 'comments'])]
protected \DateTimeImmutable $createdAt;
#[ORM\JoinColumn(name: 'ip_address_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: IpAddress::class, inversedBy: 'saloonComments')]
protected ?IpAddress $ipAddress;
public function __construct(Saloon $saloon, ?Comment $parent, ?User $user, string $text, \DateTimeImmutable $createdAt, ?string $author = null)
{
$this->parent = $parent;
$this->saloon = $saloon;
$this->user = $user;
$this->text = $text;
$this->author = $this->normalizeAuthor($author);
$this->createdAt = $createdAt;
}
public function getId(): int
{
return $this->id;
}
public function getParent(): ?Comment
{
return $this->parent;
}
public function getUser(): ?User
{
return $this->user;
}
public function getSaloon(): ?Saloon
{
return $this->saloon;
}
public function getText(): string
{
return $this->text;
}
public function getAuthorName(): ?string
{
return $this->user?->getNickName() ?? $this->author;
}
public function setText(string $text): void
{
$this->text = $text;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}
public function clearRelations(): void
{
$this->saloon = null;
$this->user = null;
}
public function getIpAddress(): ?IpAddress
{
return $this->ipAddress;
}
private function normalizeAuthor(?string $author): ?string
{
if (null === $author || '' === trim($author)) {
return null;
}
$author = trim($author);
if (64 < mb_strlen($author)) {
throw new \DomainException('Comment author name must not exceed 64 characters');
}
return $author;
}
}