<?php
namespace App\Entity;
use App\Repository\ArticleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass=ArticleRepository::class)
* @ORM\Table(
* name="article",
* uniqueConstraints={@ORM\UniqueConstraint(columns={"comingFrom", "comingFromId"})}
* )
* @UniqueEntity(fields={"comingFrom", "comingFromId"}, message="Id is already taken.")
*/
class Article
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $url;
/**
* @ORM\Column(type="string", length=512, nullable=true)
*/
private $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $language;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=10000, nullable=true)
*/
private $keywords;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $content;
/**
* @ORM\Column(type="string", length=512, nullable=true)
*/
private $categories;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $tags;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $thumbnail;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $author;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $type;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ressourceLink;
/**
* @ORM\Column(name="comingFrom", type="string", length=255)
*/
private $comingFrom;
/**
* @ORM\Column(name="comingFromId", type="integer")
*/
private $comingFromId;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $vues;
/**
* @ORM\OneToMany(targetEntity=ArticlesFavoris::class, mappedBy="article", orphanRemoval=true)
*/
private $favorites;
/**
* @ORM\OneToMany(targetEntity=Commentaire::class, mappedBy="article")
*/
private $commentaires;
private $fichiers;
public function __construct()
{
$this->favorites = new ArrayCollection();
$this->commentaires = new ArrayCollection();
$this->fichiers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setLanguage(string $language): self
{
$this->language = $language;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getKeywords(): ?string
{
return $this->keywords;
}
public function setKeywords(?string $keywords): self
{
$this->keywords = $keywords;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
public function getCategories(): ?string
{
return $this->categories;
}
public function setCategories(?string $categories): self
{
$this->categories = $categories;
return $this;
}
public function getTags(): ?string
{
return $this->tags;
}
public function setTags(?string $tags): self
{
$this->tags = $tags;
return $this;
}
public function getThumbnail(): ?string
{
return $this->thumbnail;
}
public function setThumbnail(?string $thumbnail): self
{
$this->thumbnail = $thumbnail;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(?\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getAuthor(): ?string
{
return $this->author;
}
public function setAuthor(?string $author): self
{
$this->author = $author;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getRessourceLink(): ?string
{
return $this->ressourceLink;
}
public function setRessourceLink(?string $ressourceLink): self
{
$this->ressourceLink = $ressourceLink;
return $this;
}
public function getComingFrom(): ?string
{
return $this->comingFrom;
}
public function setComingFrom(string $comingFrom): self
{
$this->comingFrom = $comingFrom;
return $this;
}
public function getComingFromId(): ?int
{
return $this->comingFromId;
}
public function setComingFromId(int $comingFromId): self
{
$this->comingFromId = $comingFromId;
return $this;
}
public function getVues(): ?int
{
return $this->vues;
}
public function setVues(?int $vues): self
{
$this->vues = $vues;
return $this;
}
/*
Permet de mettre l'article sous forme d'array
*/
public function toArray(): Array
{
$tmp = [
'id' => $this->id,
'url' => $this->url,
'title' => $this->title,
'language' => $this->language,
'description' => $this->description,
'keywords' => $this->keywords,
'content' => $this->content,
'categories' => $this->categories,
'tags' => $this->tags,
'thumbnail' => $this->thumbnail,
'date' => $this->date,
'author' => $this->author,
'type' => $this->type,
'ressourceLink' => $this->ressourceLink,
'comingFrom' => $this->comingFrom,
'comingFromId' => $this->comingFromId,
'vues' => $this->vues,
'nbRep' => count($this->commentaires),
'favorites' => [],
'fichiers' => [],
];
foreach($this->getFavorites() as $favoris)
{
$tmp['favorites'][] = $favoris->toArray();
}
foreach($this->getCommentaires() as $commentaire)
{
//get files of the comment
foreach($commentaire->getFichiers() as $fichier)
{
$tmp['fichiers'][] = $fichier->toArray();
}
}
return $tmp;
}
/*
Permet de recuperrer le to array sans les favoris, ce qui permet de ne pas boucler dans les appelle des toArray()
ce qui fait une boucle infinie
*/
public function toArrayWithoutFavoris(): Array
{
$tmp = [
'id' => $this->id,
'url' => $this->url,
'title' => $this->title,
'language' => $this->language,
'description' => $this->description,
'keywords' => $this->keywords,
'content' => $this->content,
'categories' => $this->categories,
'tags' => $this->tags,
'thumbnail' => $this->thumbnail,
'date' => $this->date,
'author' => $this->author,
'type' => $this->type,
'ressourceLink' => $this->ressourceLink,
'comingFrom' => $this->comingFrom,
'comingFromId' => $this->comingFromId,
'vues' => $this->vues,
];
return $tmp;
}
/**
* @return Collection|ArticlesFavoris[]
*/
public function getFavorites(): Collection
{
return $this->favorites;
}
public function addFavorite(ArticlesFavoris $favorite): self
{
if (!$this->favorites->contains($favorite)) {
$this->favorites[] = $favorite;
$favorite->setArticle($this);
}
return $this;
}
public function removeFavorite(ArticlesFavoris $favorite): self
{
if ($this->favorites->removeElement($favorite)) {
// set the owning side to null (unless already changed)
if ($favorite->getArticle() === $this) {
$favorite->setArticle(null);
}
}
return $this;
}
/**
* @return Collection|Commentaire[]
*/
public function getCommentaires(): Collection
{
return $this->commentaires;
}
public function addCommentaire(Commentaire $commentaire): self
{
if (!$this->commentaires->contains($commentaire)) {
$this->commentaires[] = $commentaire;
$commentaire->setArticle($this);
}
return $this;
}
public function removeCommentaire(Commentaire $commentaire): self
{
if ($this->commentaires->removeElement($commentaire)) {
// set the owning side to null (unless already changed)
if ($commentaire->getArticle() === $this) {
$commentaire->setArticle(null);
}
}
return $this;
}
public function getFiles(): ?array
{
$tmp = [];
foreach($this->getCommentaires() as $commentaire)
{
//get files of the comment
foreach($commentaire->getFichiers() as $fichier)
{
$tmp[] = $fichier->toArray();
}
}
return $tmp;
}
}