<?php
namespace App\Entity;
use App\Translation\AutoTranslatableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
#[ORM\Table(name: 'post')]
#[ORM\Index(name: 'ix_image_url', columns: ['image_url'])]
#[ORM\Index(name: 'ix_category', columns: ['category'])]
#[ORM\Entity(repositoryClass: 'App\Entity\Repository\PostRepository')]
class Post implements TranslatableInterface
{
use AutoTranslatableTrait;
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
#[ORM\Column(type: 'text', length: 0, nullable: false)]
private $text;
#[ORM\Column(type: 'string', nullable: true)]
private $image_url;
#[ORM\Column(type: 'json', nullable: true)]
private $tags;
#[ORM\Column(type: 'datetime', nullable: false)]
private $date_created;
#[ORM\OneToMany(targetEntity: 'App\Entity\PostComment', mappedBy: 'post')]
private $postComment;
#[ORM\OneToMany(targetEntity: 'App\Entity\PostBookmark', mappedBy: 'post')]
private $postBookmark;
#[ORM\ManyToOne(targetEntity: 'App\Entity\User', inversedBy: 'post')]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false)]
private $user;
#[ORM\OneToMany(targetEntity: PostLike::class, mappedBy: 'post', orphanRemoval: true)]
private $postLikes;
#[ORM\Column(type: 'json', nullable: true)]
private $richtext = [];
#[ORM\Column(type: 'string', length: 255)]
private $category;
#[ORM\Column(type: 'array', nullable: true)]
private $imageUrls = [];
#[ORM\Column(type: 'string', length: 50, nullable: true)]
private $type;
#[ORM\OneToOne(targetEntity: Content::class, mappedBy: 'post', cascade: ['persist', 'remove'])]
private $content;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $dynamic_link;
#[ORM\OneToMany(targetEntity: PostTag::class, mappedBy: 'post', orphanRemoval: true)]
private $postTags;
#[ORM\Column(length: 3, nullable: true)]
private ?string $language = null;
#[ORM\Column(nullable: true)]
private ?array $metadata = [];
#[ORM\OneToMany(mappedBy: 'post', targetEntity: PostReport::class, orphanRemoval: true)]
private Collection $postReports;
#[ORM\Column(nullable: true)]
private ?bool $hidden = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $location;
#[ORM\Column(nullable: true)]
private ?array $ai_moderation_result = null;
public function __construct()
{
$this->postComment = new ArrayCollection();
$this->postBookmark = new ArrayCollection();
$this->postLikes = new ArrayCollection();
$this->date_created = new \DateTime();
$this->category = 'default';
$this->postTags = new ArrayCollection();
$this->postReports = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getText(): ?string
{
if ($this->shouldNotBeTranslated()) {
return $this->text;
}
$translated = $this->translate()->getText();
return !empty($translated) ? $translated : $this->text;
}
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getImageUrl(): ?string
{
return $this->image_url;
}
public function setImageUrl(?string $image_url): self
{
$this->image_url = $image_url;
return $this;
}
public function getDateCreated(): ?\DateTimeInterface
{
return $this->date_created;
}
public function setDateCreated(\DateTimeInterface $date_created): self
{
$this->date_created = $date_created;
return $this;
}
/**
* @return Collection|PostComment[]
*/
public function getPostComment(): Collection
{
return $this->postComment;
}
public function addPostComment(PostComment $postComment): self
{
if (!$this->postComment->contains($postComment)) {
$this->postComment[] = $postComment;
$postComment->setPost($this);
}
return $this;
}
public function removePostComment(PostComment $postComment): self
{
if ($this->postComment->contains($postComment)) {
$this->postComment->removeElement($postComment);
// set the owning side to null (unless already changed)
if ($postComment->getPost() === $this) {
$postComment->setPost(null);
}
}
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection|PostBookmark[]
*/
public function getPostBookmark(): Collection
{
return $this->postBookmark;
}
public function addPostBookmark(PostBookmark $postBookmark): self
{
if (!$this->postBookmark->contains($postBookmark)) {
$this->postBookmark[] = $postBookmark;
$postBookmark->setPost($this);
}
return $this;
}
public function removePostBookmark(PostBookmark $postBookmark): self
{
if ($this->postBookmark->contains($postBookmark)) {
$this->postBookmark->removeElement($postBookmark);
// set the owning side to null (unless already changed)
if ($postBookmark->getPost() === $this) {
$postBookmark->setPost(null);
}
}
return $this;
}
public function getTags(): ?array
{
return $this->tags;
}
public function setTags(?array $tags): self
{
$this->tags = $tags;
return $this;
}
/**
* @return Collection|PostLike[]
*/
public function getPostLikes(): Collection
{
return $this->postLikes;
}
public function addPostLike(PostLike $postLike): self
{
if (!$this->postLikes->contains($postLike)) {
$this->postLikes[] = $postLike;
$postLike->setPost($this);
}
return $this;
}
public function removePostLike(PostLike $postLike): self
{
if ($this->postLikes->removeElement($postLike)) {
// set the owning side to null (unless already changed)
if ($postLike->getPost() === $this) {
$postLike->setPost(null);
}
}
return $this;
}
public function getRichtext(bool $skipTranslation = false): ?array
{
if ($skipTranslation || $this->shouldNotBeTranslated()) {
return $this->richtext;
}
$translated = $this->translate()->getRichtext();
return !empty($translated) ? $translated : $this->richtext;
}
public function setRichtext(?array $richtext): self
{
$this->richtext = $richtext;
return $this;
}
public function getCategory(): ?string
{
return $this->category;
}
public function setCategory(string $category): self
{
$this->category = $category;
return $this;
}
public function getImageUrls(): ?array
{
return $this->imageUrls;
}
public function setImageUrls(?array $imageUrls): self
{
$this->imageUrls = $imageUrls;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getContent(): ?Content
{
return $this->content;
}
public function setContent(?Content $content): self
{
// unset the owning side of the relation if necessary
if ($content === null && $this->content !== null) {
$this->content->setPost(null);
}
// set the owning side of the relation if necessary
if ($content !== null && $content->getPost() !== $this) {
$content->setPost($this);
}
$this->content = $content;
return $this;
}
/**
* Gets the engagement as single number: comments + likes + comment likes
*
* @return int
*/
public function getEngagement(): int
{
$score = $this->getPostComment()->count() + $this->getPostLikes()->count();
foreach ($this->getPostComment() as $comment) {
$score += $comment->getCommentLikes()->count();
}
return $score;
}
public function getDynamicLink(): ?string
{
return $this->dynamic_link;
}
public function setDynamicLink(?string $dynamic_link): self
{
$this->dynamic_link = $dynamic_link;
return $this;
}
/**
* @return Collection<int, PostTag>
*/
public function getPostTags(): Collection
{
return $this->postTags;
}
public function addPostTag(PostTag $postTag): self
{
if (!$this->postTags->contains($postTag)) {
$this->postTags[] = $postTag;
$postTag->setPost($this);
}
return $this;
}
public function removePostTag(PostTag $postTag): self
{
if ($this->postTags->removeElement($postTag)) {
// set the owning side to null (unless already changed)
if ($postTag->getPost() === $this) {
$postTag->setPost(null);
}
}
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setLanguage(?string $language): static
{
$this->language = $language;
return $this;
}
public function getMetadata(): ?array
{
return $this->metadata;
}
public function setMetadata(?array $metadata): static
{
$this->metadata = $metadata;
return $this;
}
/**
* @return Collection<int, PostReport>
*/
public function getPostReports(): Collection
{
return $this->postReports;
}
public function addPostReport(PostReport $postReport): static
{
if (!$this->postReports->contains($postReport)) {
$this->postReports->add($postReport);
$postReport->setPost($this);
}
return $this;
}
public function removePostReport(PostReport $postReport): static
{
if ($this->postReports->removeElement($postReport)) {
// set the owning side to null (unless already changed)
if ($postReport->getPost() === $this) {
$postReport->setPost(null);
}
}
return $this;
}
public function isHidden(): ?bool
{
return $this->hidden;
}
public function setHidden(?bool $hidden): static
{
$this->hidden = $hidden;
return $this;
}
public function getLocation(): ?string
{
return $this->location;
}
public function setLocation(?string $location): static
{
$this->location = $location;
return $this;
}
public function getAiModerationResult(): ?array
{
return $this->ai_moderation_result;
}
public function setAiModerationResult(?array $ai_moderation_result): static
{
$this->ai_moderation_result = $ai_moderation_result;
return $this;
}
}