src/Entity/BasePost.php line 203

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. // use Symfony\Component\String\Slugger\AsciiSlugger;
  8. use function Symfony\Component\String\u;
  9. use Symfony\Component\String\UnicodeString;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. /**
  14.  * @ORM\Entity()
  15.  * @UniqueEntity("name")
  16.  */
  17. trait BasePost
  18. {
  19.     /**
  20.      * @ORM\Id()
  21.      * @ORM\GeneratedValue()
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     protected $id;
  25.     /**
  26.      * @ORM\Column(type="array", length=255)
  27.      *
  28.      * @var array
  29.      */
  30.     private $tmpOldIds = [];
  31.     /**
  32.      * @ORM\Column(type="string", length=255)
  33.      */
  34.     protected $name;
  35.     /**
  36.      * @ORM\Column(type="string", length=255)
  37.      * @Groups({"searchable"})
  38.      */
  39.     protected $title;
  40.     /**
  41.      * @Gedmo\Slug(fields={"name"})
  42.      * @ORM\Column(type="string", length=255, unique=true)
  43.      */
  44.     protected $slug;
  45.     /**
  46.      * @ORM\Column(type="text", nullable=true)
  47.      */
  48.     protected $chapo;
  49.     /**
  50.      * @ORM\Column(type="text")
  51.      */
  52.     protected $content;
  53.     
  54.     /**
  55.      * @ORM\Column(type="text", nullable=true)
  56.      */
  57.     protected $tmpKeywords// devrait disparaitre puisqu'il est remplacé par les tags
  58.     /**
  59.      * @ORM\Column(type="integer", nullable=true)
  60.      * @Groups({"searchable"})
  61.      * @Assert\Length(
  62.      *      max = 8,
  63.      *      maxMessage = "Vous ne pouvez pas mettre un nombre de plus de {{ limit }} chiffres"
  64.      * )
  65.      */
  66.     protected $priority;
  67.     /**
  68.      * @ORM\Column(type="boolean", nullable=true)
  69.      */
  70.     protected $published;
  71.     
  72.     /**
  73.      * @var \DateTime $createdAt
  74.      *
  75.      * @Gedmo\Timestampable(on="create")
  76.      * @ORM\Column(type="datetime", nullable=true)
  77.      */
  78.     protected $createdAt;
  79.     /**
  80.      * @var \DateTime $updated
  81.      *
  82.      * @Gedmo\Timestampable(on="update")
  83.      * @ORM\Column(type="datetime", nullable=true)
  84.      */
  85.     protected $updatedAt;
  86.     /* relations */
  87.     /**
  88.      * @ORM\ManyToMany(targetEntity="App\Entity\Tag")
  89.      * @ORM\OrderBy({"name" = "ASC"})
  90.      */
  91.     private $tags;
  92.     public function __construct()
  93.     {
  94.         $this->tmpOldIds = [];
  95.         $this->title $this->name;
  96.         // $slugger = new AsciiSlugger();
  97.         // $this->slug = $slugger->slug($this->name);
  98.         $this->createdAt = new \DateTime('now'); // default 
  99.         $this->updatedAt = new \DateTime('now'); // default
  100.         $this->tags = new ArrayCollection();
  101.     }
  102.     public function getId(): ?int
  103.     {
  104.         return $this->id;
  105.     }
  106.     public function getTmpOldIds(): ?array
  107.     {
  108.         return $this->tmpOldIds;
  109.     }
  110.     public function setTmpOldIds(array $types): self
  111.     {
  112.         $this->tmpOldIds $tmpOldIds;
  113.         return $this;
  114.     }
  115.     public function addTmpOldId(int $tmpOldId): self
  116.     {
  117.         // on ramène à 5 chiffres pour éviter les doubles 
  118.         while( strlen( (string) $tmpOldId) < $tmpOldId "0" $tmpOldId;
  119.         
  120.         if(! in_array($tmpOldId$this->tmpOldIds) ) {
  121.             // print "added " . $tmpOldId;
  122.             // dump($this->tmpOldIds);
  123.             $this->tmpOldIds[] = $tmpOldId;
  124.         }
  125.         return $this;
  126.     }
  127.     public function getName(): ?string
  128.     {
  129.         return $this->name;
  130.     }
  131.     public function setName(string $name): self
  132.     {
  133.         $this->name $name;
  134.         return $this;
  135.     }
  136.     public function getTitle(): ?string
  137.     {
  138.         return $this->title;
  139.     }
  140.     public function setTitle(string $title): self
  141.     {
  142.         $this->title $title;
  143.         return $this;
  144.     }
  145.     public function getSlug(): ?string
  146.     {
  147.         return $this->slug;
  148.     }
  149.     public function setSlug(string $slug): self
  150.     {
  151.         $this->slug $slug;
  152.         return $this;
  153.     }
  154.     /**
  155.      * @Groups({"searchable"})
  156.      */
  157.     public function getChapoForSearch(): ?string
  158.     {
  159.         $str $this->chapo;
  160.         $str u($str)
  161.             ->normalize(UnicodeString::NFKC)
  162.         ;
  163.         $str strip_tags($str);
  164.         return $str;
  165.     }
  166.     public function getChapo(): ?string
  167.     {
  168.         return html_entity_decode($this->chapoENT_QUOTES);
  169.     }
  170.     public function setChapo(?string $chapo): self
  171.     {
  172.         $this->chapo $chapo;
  173.         return $this;
  174.     }
  175.     /**
  176.      * @Groups({"searchable"})
  177.      */
  178.     public function getContentForSearch(): ?string
  179.     {
  180.         $str $this->content;
  181.         $str u($str)
  182.             ->normalize(UnicodeString::NFKC)
  183.             ->slice(09000)
  184.         ;
  185.         $str strip_tags($str);
  186.         
  187.         return $str;
  188.     }
  189.     public function getContent(): ?string
  190.     {
  191.         return html_entity_decode($this->contentENT_QUOTES);
  192.     }
  193.     public function setContent(string $content): self
  194.     {
  195.         $this->content $content;
  196.         return $this;
  197.     }
  198.     public function getTmpKeywords(): ?string
  199.     {
  200.         return $this->tmpKeywords;
  201.     }
  202.     public function setTmpKeywords(?string $tmpKeywords): self
  203.     {
  204.         $this->tmpKeywords $tmpKeywords;
  205.         return $this;
  206.     }
  207.     public function getPriority(): ?int
  208.     {
  209.         return $this->priority;
  210.     }
  211.     public function setPriority(?int $priority): self
  212.     {
  213.         $this->priority $priority;
  214.         return $this;
  215.     }
  216.     public function isPublished(): ?bool
  217.     {
  218.         return $this->published;
  219.         // return !is_null($this->published_at);
  220.     }
  221.     public function getPublished(): ?bool
  222.     {
  223.         return $this->published;
  224.     }
  225.     public function setPublished(?bool $published): self
  226.     {
  227.         $this->published $published;
  228.         return $this;
  229.     }
  230.     public function getCreatedAt(): ?\DateTimeInterface
  231.     {
  232.         return $this->createdAt;
  233.     }
  234.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  235.     {
  236.         $this->createdAt $createdAt;
  237.         return $this;
  238.     }
  239.     public function getUpdatedAt(): ?\DateTimeInterface
  240.     {
  241.         return $this->updatedAt;
  242.     }
  243.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  244.     {
  245.         $this->updatedAt $updatedAt;
  246.         return $this;
  247.     }
  248.     
  249.     /**
  250.      * @return Collection|Tag[]
  251.      */
  252.     public function getTags(): ?Collection
  253.     {
  254.         return $this->tags;
  255.     }
  256.     public function addTag(Tag $tag): self
  257.     {
  258.         if (!$this->tags->contains($tag)) {
  259.             $this->tags[] = $tag;
  260.         }
  261.         return $this;
  262.     }
  263.     public function removeTag(Tag $tag): self
  264.     {
  265.         if ($this->tags->contains($tag)) {
  266.             $this->tags->removeElement($tag);
  267.         }
  268.         return $this;
  269.     }
  270.     
  271. }