<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Validator as AppAssert;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
* @ORM\HasLifecycleCallbacks()
* @Vich\Uploadable
* @AppAssert\DynamicValidationGroups(property="type", value="App\Enum\ArticleType::INTERNAL", validationGroups={"TypeInternal"})
* @AppAssert\DynamicValidationGroups(property="type", value="App\Enum\ArticleType::EXTERNAL", validationGroups={"TypeExternal"})
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['article:read']],
denormalizationContext: ['groups' => ['article:write']],
collectionOperations: [
'get',
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
'input_formats' => [
'multipart' => ['multipart/form-data'],
],
],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'title' => 'partial', 'type' => 'exact'])]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title', 'type', 'published', 'announcement'], arguments: ['orderParameterName' => 'order'])]
class Article
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=128)
* @Assert\NotBlank()
* @Assert\Length(max="128")
* @Groups({"article:read", "article:write"})
*/
public $title;
/**
* @ORM\Column(type="string", length=128)
* @Gedmo\Slug(fields={"title"})
* @Groups({"article:read", "article:write"})
*/
public $slug;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"article:read", "article:write"})
*/
public $author;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
* @Groups({"article:read", "article:write"})
*/
public $excerpt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="articles")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Ignore()
* @Groups({"article:read", "article:write"})
*/
private $company;
#[Groups(['article:read'])]
public ?string $contentUrl = null;
/**
* @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
*/
public $image;
/**
* @Vich\UploadableField(mapping="article_image", fileNameProperty="image.name",
* size="image.size", mimeType="image.mimeType",
* originalName="image.originalName",
* dimensions="image.dimensions")
* @Assert\Image(
* mimeTypes={"image/jpeg", "image/png", "image/gif"},
* mimeTypesMessage="Allowed formats : .png, .jpeg, .jpg, gif "
* )
*/
#[Groups(['article:write'])]
public ?File $file = null;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
* @Groups({"article:read", "article:write"})
*/
private $imageAlt;
/**
* @var string
*
* @ORM\Column(type="string", length=64, nullable=false)
* @Assert\Length(max=64)
* @EnumAssert(class="App\Enum\ArticleType")
* @Assert\NotBlank()
* @Groups({"article:read", "article:write"})
*/
private $type;
/**
* @ORM\Column(type="text", nullable=true)
* @Assert\NotBlank(groups={"TypeInternal"})
* @Groups({"article:read", "article:write"})
*/
private $content;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Url()
* @Assert\NotBlank(groups={"TypeExternal"})
* @Groups({"article:read", "article:write"})
*/
private $externalLink;
/**
* @ORM\Column(type="boolean")
* @Assert\Type("bool")
* @Groups({"article:read", "article:write"})
*/
private $published;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserLike", mappedBy="article", cascade={"persist"})
* @Groups({"article:read", "article:write"})
*/
private $userLikes;
/**
* @ORM\Column(type="integer")
* @Groups({"article:read", "article:write"})
*/
private $nbLikes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserComment", mappedBy="article", orphanRemoval=true, cascade={"persist"})
* @Groups({"article:read", "article:write"})
*/
private $userComments;
/**
* @ORM\Column(type="integer")
* @Groups({"article:read", "article:write"})
*/
private $nbComments;
/**
* @ORM\Column(type="integer")
* @Groups({"article:read", "article:write"})
*/
private $nbShares;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserShare", mappedBy="article", cascade={"persist"})
* @Groups({"article:read", "article:write"})
*/
private $userShares;
/**
* @ORM\OneToMany(targetEntity="App\Entity\FeedItem", cascade={"persist"}, mappedBy="article", orphanRemoval=true)
*/
private $feedItems;
/**
* @ORM\Column(type="boolean")
* @Assert\Type("bool")
*/
private $highlighted;
/**
* @ORM\Column(type="boolean")
* @Assert\Type("bool")
* @Groups({"article:read", "article:write"})
*/
private $announcement;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\Benefit")
* @Groups({"article:read", "article:write"})
*/
private $benefits;
/**
* @var ArrayCollection|SocialGroupIssue[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\SocialGroupIssue")
*/
private $socialGroupIssues;
/**
* @ORM\Column(type="boolean")
* @Assert\Type("bool")
*/
private $isDiNews;
/**
* Article constructor.
*/
public function __construct()
{
$this->published = true;
$this->image = new EmbeddedFile();
$this->userLikes = new ArrayCollection();
$this->nbLikes = 0;
$this->userComments = new ArrayCollection();
$this->nbComments = 0;
$this->userShares = new ArrayCollection();
$this->nbShares = 0;
$this->feedItems = new ArrayCollection();
$this->highlighted = false;
$this->announcement = false;
$this->benefits = new ArrayCollection();
$this->socialGroupIssues = new ArrayCollection();
$this->isDiNews = false;
}
public function __toString()
{
return $this->title;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getAuthor(): ?string
{
return $this->author;
}
public function setAuthor(?string $author): self
{
$this->author = $author;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
public function getExcerpt(): ?string
{
return $this->excerpt;
}
public function setExcerpt(string $excerpt): self
{
$this->excerpt = $excerpt;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getPublished(): ?bool
{
return $this->published;
}
public function setPublished(bool $published): self
{
$this->published = $published;
return $this;
}
public function getExternalLink(): ?string
{
return $this->externalLink;
}
public function setExternalLink(?string $externalLink): self
{
$this->externalLink = $externalLink;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @throws Exception
*/
public function setImageFile(?File $image): self
{
$this->imageFile = $image;
if ($image) {
$this->updatedAt = new DateTime();
}
return $this;
}
public function getImage(): EmbeddedFile
{
return $this->image;
}
public function setImage(EmbeddedFile $image): self
{
$this->image = $image;
return $this;
}
public function setImageAlt(?string $imageAlt): self
{
$this->imageAlt = $imageAlt;
return $this;
}
public function getImageAlt(): ?string
{
return $this->imageAlt;
}
/**
* @return Collection|UserLike[]
*/
public function getUserLikes(): Collection
{
return $this->userLikes;
}
public function addUserLike(UserLike $userLike): self
{
if (!$this->userLikes->contains($userLike)) {
$this->userLikes[] = $userLike;
$userLike->setArticle($this);
}
return $this;
}
public function removeUserLike(UserLike $userLike): self
{
if ($this->userLikes->contains($userLike)) {
$this->userLikes->removeElement($userLike);
// set the owning side to null (unless already changed)
if ($userLike->getArticle() === $this) {
$userLike->setArticle(null);
}
}
return $this;
}
public function getNbLikes(): ?int
{
return $this->nbLikes;
}
public function setNbLikes(int $nbLikes): self
{
$this->nbLikes = $nbLikes;
return $this;
}
/**
* @return Collection|UserComment[]
*/
public function getUserComments(): Collection
{
return $this->userComments;
}
public function addUserComment(UserComment $userComment): self
{
if (!$this->userComments->contains($userComment)) {
$this->userComments[] = $userComment;
$userComment->setArticle($this);
}
return $this;
}
public function removeUserComment(UserComment $userComment): self
{
if ($this->userComments->contains($userComment)) {
$this->userComments->removeElement($userComment);
// set the owning side to null (unless already changed)
if ($userComment->getArticle() === $this) {
$userComment->setArticle(null);
}
}
return $this;
}
public function getNbComments(): int
{
return $this->nbComments;
}
public function setNbComments(int $nbComments): self
{
$this->nbComments = $nbComments;
return $this;
}
/**
* @return Collection|UserShare[]
*/
public function getUserShares(): Collection
{
return $this->userShares;
}
public function addUserShare(UserShare $userShare): self
{
if (!$this->userShares->contains($userShare)) {
$this->userShares[] = $userShare;
$userShare->setArticle($this);
}
return $this;
}
public function removeUserShare(UserShare $userShare): self
{
if ($this->userShares->contains($userShare)) {
$this->userShares->removeElement($userShare);
// set the owning side to null (unless already changed)
if ($userShare->getArticle() === $this) {
$userShare->setArticle(null);
}
}
return $this;
}
public function getNbShares(): ?int
{
return $this->nbShares;
}
public function setNbShares(int $nbShares): self
{
$this->nbShares = $nbShares;
return $this;
}
/**
* @return Collection|FeedItem[]
*/
public function getFeedItems(): Collection
{
return $this->feedItems;
}
public function addFeedItem(FeedItem $feedItem): self
{
if (!$this->feedItems->contains($feedItem)) {
$this->feedItems[] = $feedItem;
$feedItem->setArticle($this);
}
return $this;
}
public function removeFeedItem(FeedItem $feedItem): self
{
if ($this->feedItems->contains($feedItem)) {
$this->feedItems->removeElement($feedItem);
// set the owning side to null (unless already changed)
if ($feedItem->getArticle() === $this) {
$feedItem->setArticle(null);
}
}
return $this;
}
public function getHighlighted(): ?bool
{
return $this->highlighted;
}
public function setHighlighted(bool $highlighted): self
{
$this->highlighted = $highlighted;
return $this;
}
public function getAnnouncement(): ?bool
{
return $this->announcement;
}
public function setAnnouncement(bool $announcement): self
{
$this->announcement = $announcement;
return $this;
}
/**
* @return Collection|Benefit[]
*/
public function getBenefits(): Collection
{
return $this->benefits;
}
public function addBenefit(Benefit $benefit): self
{
if (!$this->benefits->contains($benefit)) {
$this->benefits[] = $benefit;
}
return $this;
}
public function removeBenefit(Benefit $benefit): self
{
if ($this->benefits->contains($benefit)) {
$this->benefits->removeElement($benefit);
}
return $this;
}
/**
* @return Collection|SocialGroupIssue[]
*/
public function getSocialGroupIssues(): Collection
{
return $this->socialGroupIssues;
}
public function addSocialGroupIssue(SocialGroupIssue $socialGroupIssue): self
{
if (!$this->socialGroupIssues->contains($socialGroupIssue)) {
$this->socialGroupIssues[] = $socialGroupIssue;
}
return $this;
}
public function removeSocialGroupIssue(SocialGroupIssue $socialGroupIssue): self
{
if ($this->socialGroupIssues->contains($socialGroupIssue)) {
$this->socialGroupIssues->removeElement($socialGroupIssue);
}
return $this;
}
public function getIsDiNews(): ?bool
{
return $this->isDiNews;
}
public function setIsDiNews(bool $isDiNews): self
{
$this->isDiNews = $isDiNews;
return $this;
}
}