<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
// use Symfony\Component\String\Slugger\AsciiSlugger;
use function Symfony\Component\String\u;
use Symfony\Component\String\UnicodeString;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity()
* @UniqueEntity("name")
*/
trait BasePost
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="array", length=255)
*
* @var array
*/
private $tmpOldIds = [];
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"searchable"})
*/
protected $title;
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", length=255, unique=true)
*/
protected $slug;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $chapo;
/**
* @ORM\Column(type="text")
*/
protected $content;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $tmpKeywords; // devrait disparaitre puisqu'il est remplacé par les tags
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"searchable"})
* @Assert\Length(
* max = 8,
* maxMessage = "Vous ne pouvez pas mettre un nombre de plus de {{ limit }} chiffres"
* )
*/
protected $priority;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $published;
/**
* @var \DateTime $createdAt
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime", nullable=true)
*/
protected $createdAt;
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime", nullable=true)
*/
protected $updatedAt;
/* relations */
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Tag")
* @ORM\OrderBy({"name" = "ASC"})
*/
private $tags;
public function __construct()
{
$this->tmpOldIds = [];
$this->title = $this->name;
// $slugger = new AsciiSlugger();
// $this->slug = $slugger->slug($this->name);
$this->createdAt = new \DateTime('now'); // default
$this->updatedAt = new \DateTime('now'); // default
$this->tags = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTmpOldIds(): ?array
{
return $this->tmpOldIds;
}
public function setTmpOldIds(array $types): self
{
$this->tmpOldIds = $tmpOldIds;
return $this;
}
public function addTmpOldId(int $tmpOldId): self
{
// on ramène à 5 chiffres pour éviter les doubles
while( strlen( (string) $tmpOldId) < 5 ) $tmpOldId = "0" . $tmpOldId;
if(! in_array($tmpOldId, $this->tmpOldIds) ) {
// print "added " . $tmpOldId;
// dump($this->tmpOldIds);
$this->tmpOldIds[] = $tmpOldId;
}
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @Groups({"searchable"})
*/
public function getChapoForSearch(): ?string
{
$str = $this->chapo;
$str = u($str)
->normalize(UnicodeString::NFKC)
;
$str = strip_tags($str);
return $str;
}
public function getChapo(): ?string
{
return html_entity_decode($this->chapo, ENT_QUOTES);
}
public function setChapo(?string $chapo): self
{
$this->chapo = $chapo;
return $this;
}
/**
* @Groups({"searchable"})
*/
public function getContentForSearch(): ?string
{
$str = $this->content;
$str = u($str)
->normalize(UnicodeString::NFKC)
->slice(0, 9000)
;
$str = strip_tags($str);
return $str;
}
public function getContent(): ?string
{
return html_entity_decode($this->content, ENT_QUOTES);
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getTmpKeywords(): ?string
{
return $this->tmpKeywords;
}
public function setTmpKeywords(?string $tmpKeywords): self
{
$this->tmpKeywords = $tmpKeywords;
return $this;
}
public function getPriority(): ?int
{
return $this->priority;
}
public function setPriority(?int $priority): self
{
$this->priority = $priority;
return $this;
}
public function isPublished(): ?bool
{
return $this->published;
// return !is_null($this->published_at);
}
public function getPublished(): ?bool
{
return $this->published;
}
public function setPublished(?bool $published): self
{
$this->published = $published;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTags(): ?Collection
{
return $this->tags;
}
public function addTag(Tag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
}
return $this;
}
public function removeTag(Tag $tag): self
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
}
return $this;
}
}