src/Entity/Article.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ArticleRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. /**
  9.  * @ORM\Entity(repositoryClass=ArticleRepository::class)
  10.  * @ORM\Table(
  11.  *      name="article",
  12.  *      uniqueConstraints={@ORM\UniqueConstraint(columns={"comingFrom", "comingFromId"})}
  13.  * )
  14.  * @UniqueEntity(fields={"comingFrom", "comingFromId"}, message="Id is already taken.")
  15.  */
  16. class Article
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\Column(type="integer")
  21.      * @ORM\GeneratedValue(strategy="AUTO")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="string", length=255, nullable=true)
  26.      */
  27.     private $url;
  28.     /**
  29.      * @ORM\Column(type="string", length=512, nullable=true)
  30.      */
  31.     private $title;
  32.     /**
  33.      * @ORM\Column(type="string", length=255, nullable=true)
  34.      */
  35.     private $language;
  36.     /**
  37.      * @ORM\Column(type="string", length=255, nullable=true)
  38.      */
  39.     private $description;
  40.     /**
  41.      * @ORM\Column(type="string", length=10000, nullable=true)
  42.      */
  43.     private $keywords;
  44.     /**
  45.      * @ORM\Column(type="text", nullable=true)
  46.      */
  47.     private $content;
  48.     /**
  49.      * @ORM\Column(type="string", length=512, nullable=true)
  50.      */
  51.     private $categories;
  52.     /**
  53.      * @ORM\Column(type="string", length=255, nullable=true)
  54.      */
  55.     private $tags;
  56.     /**
  57.      * @ORM\Column(type="string", length=255, nullable=true)
  58.      */
  59.     private $thumbnail;
  60.     /**
  61.      * @ORM\Column(type="datetime", nullable=true)
  62.      */
  63.     private $date;
  64.     /**
  65.      * @ORM\Column(type="string", length=255, nullable=true)
  66.      */
  67.     private $author;
  68.     /**
  69.      * @ORM\Column(type="string", length=255, nullable=true)
  70.      */
  71.     private $type;
  72.     /**
  73.      * @ORM\Column(type="string", length=255, nullable=true)
  74.      */
  75.     private $ressourceLink;
  76.     /**
  77.      * @ORM\Column(name="comingFrom", type="string", length=255)
  78.      */
  79.     private $comingFrom;
  80.     /**
  81.      * @ORM\Column(name="comingFromId", type="integer")
  82.      */
  83.     private $comingFromId;
  84.     /**
  85.      * @ORM\Column(type="integer", nullable=true)
  86.      */
  87.     private $vues;
  88.     /**
  89.      * @ORM\OneToMany(targetEntity=ArticlesFavoris::class, mappedBy="article", orphanRemoval=true)
  90.      */
  91.     private $favorites;
  92.     /**
  93.      * @ORM\OneToMany(targetEntity=Commentaire::class, mappedBy="article")
  94.      */
  95.     private $commentaires;
  96.     
  97.     private $fichiers;
  98.     public function __construct()
  99.     {
  100.         $this->favorites = new ArrayCollection();
  101.         $this->commentaires = new ArrayCollection();
  102.         $this->fichiers = new ArrayCollection();
  103.     }
  104.     public function getId(): ?int
  105.     {
  106.         return $this->id;
  107.     }
  108.     public function getUrl(): ?string
  109.     {
  110.         return $this->url;
  111.     }
  112.     public function setUrl(?string $url): self
  113.     {
  114.         $this->url $url;
  115.         return $this;
  116.     }
  117.     public function getTitle(): ?string
  118.     {
  119.         return $this->title;
  120.     }
  121.     public function setTitle(?string $title): self
  122.     {
  123.         $this->title $title;
  124.         return $this;
  125.     }
  126.     public function getLanguage(): ?string
  127.     {
  128.         return $this->language;
  129.     }
  130.     public function setLanguage(string $language): self
  131.     {
  132.         $this->language $language;
  133.         return $this;
  134.     }
  135.     public function getDescription(): ?string
  136.     {
  137.         return $this->description;
  138.     }
  139.     public function setDescription(?string $description): self
  140.     {
  141.         $this->description $description;
  142.         return $this;
  143.     }
  144.     public function getKeywords(): ?string
  145.     {
  146.         return $this->keywords;
  147.     }
  148.     public function setKeywords(?string $keywords): self
  149.     {
  150.         $this->keywords $keywords;
  151.         return $this;
  152.     }
  153.     public function getContent(): ?string
  154.     {
  155.         return $this->content;
  156.     }
  157.     public function setContent(?string $content): self
  158.     {
  159.         $this->content $content;
  160.         return $this;
  161.     }
  162.     public function getCategories(): ?string
  163.     {
  164.         return $this->categories;
  165.     }
  166.     public function setCategories(?string $categories): self
  167.     {
  168.         $this->categories $categories;
  169.         return $this;
  170.     }
  171.     public function getTags(): ?string
  172.     {
  173.         return $this->tags;
  174.     }
  175.     public function setTags(?string $tags): self
  176.     {
  177.         $this->tags $tags;
  178.         return $this;
  179.     }
  180.     public function getThumbnail(): ?string
  181.     {
  182.         return $this->thumbnail;
  183.     }
  184.     public function setThumbnail(?string $thumbnail): self
  185.     {
  186.         $this->thumbnail $thumbnail;
  187.         return $this;
  188.     }
  189.     public function getDate(): ?\DateTimeInterface
  190.     {
  191.         return $this->date;
  192.     }
  193.     public function setDate(?\DateTimeInterface $date): self
  194.     {
  195.         $this->date $date;
  196.         return $this;
  197.     }
  198.     public function getAuthor(): ?string
  199.     {
  200.         return $this->author;
  201.     }
  202.     public function setAuthor(?string $author): self
  203.     {
  204.         $this->author $author;
  205.         return $this;
  206.     }
  207.     public function getType(): ?string
  208.     {
  209.         return $this->type;
  210.     }
  211.     public function setType(?string $type): self
  212.     {
  213.         $this->type $type;
  214.         return $this;
  215.     }
  216.     public function getRessourceLink(): ?string
  217.     {
  218.         return $this->ressourceLink;
  219.     }
  220.     public function setRessourceLink(?string $ressourceLink): self
  221.     {
  222.         $this->ressourceLink $ressourceLink;
  223.         return $this;
  224.     }
  225.     public function getComingFrom(): ?string
  226.     {
  227.         return $this->comingFrom;
  228.     }
  229.     public function setComingFrom(string $comingFrom): self
  230.     {
  231.         $this->comingFrom $comingFrom;
  232.         return $this;
  233.     }
  234.     public function getComingFromId(): ?int
  235.     {
  236.         return $this->comingFromId;
  237.     }
  238.     public function setComingFromId(int $comingFromId): self
  239.     {
  240.         $this->comingFromId $comingFromId;
  241.         return $this;
  242.     }
  243.     public function getVues(): ?int
  244.     {
  245.         return $this->vues;
  246.     }
  247.     public function setVues(?int $vues): self
  248.     {
  249.         $this->vues $vues;
  250.         return $this;
  251.     }
  252.     
  253.     /*
  254.     Permet de mettre l'article sous forme d'array
  255.     */
  256.     public function toArray(): Array
  257.     {
  258.             
  259.         $tmp = [
  260.             'id' => $this->id,
  261.             'url' => $this->url,
  262.             'title' => $this->title,
  263.             'language' => $this->language,
  264.             'description' => $this->description,
  265.             'keywords' => $this->keywords,
  266.             'content' => $this->content,
  267.             'categories' => $this->categories,
  268.             'tags' => $this->tags,
  269.             'thumbnail' => $this->thumbnail,
  270.             'date' => $this->date,
  271.             'author' => $this->author,
  272.             'type' => $this->type,
  273.             'ressourceLink' => $this->ressourceLink,
  274.             'comingFrom' => $this->comingFrom,
  275.             'comingFromId' => $this->comingFromId,
  276.             'vues' => $this->vues,
  277.             'nbRep' => count($this->commentaires),
  278.             'favorites' => [],
  279.             'fichiers' => [],
  280.         ];
  281.         foreach($this->getFavorites() as $favoris)
  282.         {
  283.             $tmp['favorites'][] = $favoris->toArray();
  284.         }
  285.         foreach($this->getCommentaires() as $commentaire)
  286.         {
  287.             //get files of the comment
  288.             foreach($commentaire->getFichiers() as $fichier)
  289.             {
  290.                 $tmp['fichiers'][] = $fichier->toArray();
  291.             }
  292.         }
  293.         return $tmp;
  294.     }
  295.     /*
  296.     Permet de recuperrer le to array sans les favoris, ce qui permet de ne pas boucler dans les appelle des toArray() 
  297.     ce qui fait une boucle infinie
  298.     */
  299.     public function toArrayWithoutFavoris(): Array
  300.     {
  301.             
  302.         $tmp = [
  303.             'id' => $this->id,
  304.             'url' => $this->url,
  305.             'title' => $this->title,
  306.             'language' => $this->language,
  307.             'description' => $this->description,
  308.             'keywords' => $this->keywords,
  309.             'content' => $this->content,
  310.             'categories' => $this->categories,
  311.             'tags' => $this->tags,
  312.             'thumbnail' => $this->thumbnail,
  313.             'date' => $this->date,
  314.             'author' => $this->author,
  315.             'type' => $this->type,
  316.             'ressourceLink' => $this->ressourceLink,
  317.             'comingFrom' => $this->comingFrom,
  318.             'comingFromId' => $this->comingFromId,
  319.             'vues' => $this->vues,
  320.         ];
  321.         return $tmp;
  322.     }
  323.     /**
  324.      * @return Collection|ArticlesFavoris[]
  325.      */
  326.     public function getFavorites(): Collection
  327.     {
  328.         return $this->favorites;
  329.     }
  330.     public function addFavorite(ArticlesFavoris $favorite): self
  331.     {
  332.         if (!$this->favorites->contains($favorite)) {
  333.             $this->favorites[] = $favorite;
  334.             $favorite->setArticle($this);
  335.         }
  336.         return $this;
  337.     }
  338.     public function removeFavorite(ArticlesFavoris $favorite): self
  339.     {
  340.         if ($this->favorites->removeElement($favorite)) {
  341.             // set the owning side to null (unless already changed)
  342.             if ($favorite->getArticle() === $this) {
  343.                 $favorite->setArticle(null);
  344.             }
  345.         }
  346.         return $this;
  347.     }
  348.     /**
  349.      * @return Collection|Commentaire[]
  350.      */
  351.     public function getCommentaires(): Collection
  352.     {
  353.         return $this->commentaires;
  354.     }
  355.     public function addCommentaire(Commentaire $commentaire): self
  356.     {
  357.         if (!$this->commentaires->contains($commentaire)) {
  358.             $this->commentaires[] = $commentaire;
  359.             $commentaire->setArticle($this);
  360.         }
  361.         return $this;
  362.     }
  363.     public function removeCommentaire(Commentaire $commentaire): self
  364.     {
  365.         if ($this->commentaires->removeElement($commentaire)) {
  366.             // set the owning side to null (unless already changed)
  367.             if ($commentaire->getArticle() === $this) {
  368.                 $commentaire->setArticle(null);
  369.             }
  370.         }
  371.         return $this;
  372.     }
  373.     public function getFiles(): ?array
  374.     {
  375.         $tmp = [];
  376.         foreach($this->getCommentaires() as $commentaire)
  377.         {
  378.             //get files of the comment
  379.             foreach($commentaire->getFichiers() as $fichier)
  380.             {
  381.                 $tmp[] = $fichier->toArray();
  382.             }
  383.         }
  384.         return $tmp;
  385.     }
  386.    
  387. }