src/Entity/Article.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use App\Validator as AppAssert;
  9. use DateTime;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Exception;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Gedmo\Timestampable\Traits\TimestampableEntity;
  16. use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
  17. use Symfony\Component\HttpFoundation\File\File;
  18. use Symfony\Component\Serializer\Annotation\Groups;
  19. use Symfony\Component\Serializer\Annotation\Ignore;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. use Vich\UploaderBundle\Entity\File as EmbeddedFile;
  22. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  23. /**
  24.  * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
  25.  * @ORM\HasLifecycleCallbacks()
  26.  * @Vich\Uploadable
  27.  * @AppAssert\DynamicValidationGroups(property="type", value="App\Enum\ArticleType::INTERNAL", validationGroups={"TypeInternal"})
  28.  * @AppAssert\DynamicValidationGroups(property="type", value="App\Enum\ArticleType::EXTERNAL", validationGroups={"TypeExternal"})
  29.  */
  30. #[ApiResource(
  31.     attributes: ['security' => "is_granted('ROLE_USER')"],
  32.     normalizationContext: ['groups' => ['article:read']],
  33.     denormalizationContext: ['groups' => ['article:write']],
  34.     collectionOperations: [
  35.         'get',
  36.         'post' => [
  37.             'security' => "is_granted('ROLE_ADMIN')",
  38.             'input_formats' => [
  39.                 'multipart' => ['multipart/form-data'],
  40.             ],
  41.         ],
  42.     ],
  43.     itemOperations: [
  44.         'get',
  45.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  46.     ],
  47. )]
  48. #[ApiFilter(PropertyFilter::class)]
  49. #[ApiFilter(SearchFilter::class, properties: ['id''title' => 'partial''type' => 'exact'])]
  50. #[ApiFilter(OrderFilter::class, properties: ['id''title''type''published''announcement'], arguments: ['orderParameterName' => 'order'])]
  51. class Article
  52. {
  53.     use TimestampableEntity;
  54.     /**
  55.      * @ORM\Id()
  56.      * @ORM\GeneratedValue()
  57.      * @ORM\Column(type="integer")
  58.      */
  59.     private $id;
  60.     /**
  61.      * @ORM\Column(type="string", length=128)
  62.      * @Assert\NotBlank()
  63.      * @Assert\Length(max="128")
  64.      * @Groups({"article:read", "article:write"})
  65.      */
  66.     public $title;
  67.     /**
  68.      * @ORM\Column(type="string", length=128)
  69.      * @Gedmo\Slug(fields={"title"})
  70.      * @Groups({"article:read", "article:write"})
  71.      */
  72.     public $slug;
  73.     /**
  74.      * @ORM\Column(type="string", length=255, nullable=true)
  75.      * @Groups({"article:read", "article:write"})
  76.      */
  77.     public $author;
  78.     /**
  79.      * @ORM\Column(type="text")
  80.      * @Assert\NotBlank()
  81.      * @Groups({"article:read", "article:write"})
  82.      */
  83.     public $excerpt;
  84.     /**
  85.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="articles")
  86.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  87.      * @Ignore()
  88.      * @Groups({"article:read", "article:write"})
  89.      */
  90.     private $company;
  91.     #[Groups(['article:read'])]
  92.     public ?string $contentUrl null;
  93.     /**
  94.      * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
  95.      */
  96.     public $image;
  97.     /**
  98.      * @Vich\UploadableField(mapping="article_image", fileNameProperty="image.name",
  99.      *     size="image.size", mimeType="image.mimeType",
  100.      *     originalName="image.originalName",
  101.      *     dimensions="image.dimensions")
  102.      * @Assert\Image(
  103.      *     mimeTypes={"image/jpeg", "image/png", "image/gif"},
  104.      *     mimeTypesMessage="Allowed formats : .png, .jpeg, .jpg, gif "
  105.      *     )
  106.      */
  107.     #[Groups(['article:write'])]
  108.     public ?File $file null;
  109.     /**
  110.      * @var string
  111.      *
  112.      * @ORM\Column(type="string", length=255)
  113.      * @Groups({"article:read", "article:write"})
  114.      */
  115.     private $imageAlt;
  116.     /**
  117.      * @var string
  118.      *
  119.      * @ORM\Column(type="string", length=64, nullable=false)
  120.      * @Assert\Length(max=64)
  121.      * @EnumAssert(class="App\Enum\ArticleType")
  122.      * @Assert\NotBlank()
  123.      * @Groups({"article:read", "article:write"})
  124.      */
  125.     private $type;
  126.     /**
  127.      * @ORM\Column(type="text", nullable=true)
  128.      * @Assert\NotBlank(groups={"TypeInternal"})
  129.      * @Groups({"article:read", "article:write"})
  130.      */
  131.     private $content;
  132.     /**
  133.      * @ORM\Column(type="string", length=255, nullable=true)
  134.      * @Assert\Url()
  135.      * @Assert\NotBlank(groups={"TypeExternal"})
  136.      * @Groups({"article:read", "article:write"})
  137.      */
  138.     private $externalLink;
  139.     /**
  140.      * @ORM\Column(type="boolean")
  141.      * @Assert\Type("bool")
  142.      * @Groups({"article:read", "article:write"})
  143.      */
  144.     private $published;
  145.     /**
  146.      * @ORM\OneToMany(targetEntity="App\Entity\UserLike", mappedBy="article", cascade={"persist"})
  147.      * @Groups({"article:read", "article:write"})
  148.      */
  149.     private $userLikes;
  150.     /**
  151.      * @ORM\Column(type="integer")
  152.      * @Groups({"article:read", "article:write"})
  153.      */
  154.     private $nbLikes;
  155.     /**
  156.      * @ORM\OneToMany(targetEntity="App\Entity\UserComment", mappedBy="article", orphanRemoval=true, cascade={"persist"})
  157.      * @Groups({"article:read", "article:write"})
  158.      */
  159.     private $userComments;
  160.     /**
  161.      * @ORM\Column(type="integer")
  162.      * @Groups({"article:read", "article:write"})
  163.      */
  164.     private $nbComments;
  165.     /**
  166.      * @ORM\Column(type="integer")
  167.      * @Groups({"article:read", "article:write"})
  168.      */
  169.     private $nbShares;
  170.     /**
  171.      * @ORM\OneToMany(targetEntity="App\Entity\UserShare", mappedBy="article", cascade={"persist"})
  172.      * @Groups({"article:read", "article:write"})
  173.      */
  174.     private $userShares;
  175.     /**
  176.      * @ORM\OneToMany(targetEntity="App\Entity\FeedItem", cascade={"persist"}, mappedBy="article", orphanRemoval=true)
  177.      */
  178.     private $feedItems;
  179.     /**
  180.      * @ORM\Column(type="boolean")
  181.      * @Assert\Type("bool")
  182.      */
  183.     private $highlighted;
  184.     /**
  185.      * @ORM\Column(type="boolean")
  186.      * @Assert\Type("bool")
  187.      * @Groups({"article:read", "article:write"})
  188.      */
  189.     private $announcement;
  190.     /**
  191.      * @var ArrayCollection
  192.      *
  193.      * @ORM\ManyToMany(targetEntity="App\Entity\Benefit")
  194.      * @Groups({"article:read", "article:write"})
  195.      */
  196.     private $benefits;
  197.     /**
  198.      * @var ArrayCollection|SocialGroupIssue[]
  199.      *
  200.      * @ORM\ManyToMany(targetEntity="App\Entity\SocialGroupIssue")
  201.      */
  202.     private $socialGroupIssues;
  203.     /**
  204.      * @ORM\Column(type="boolean")
  205.      * @Assert\Type("bool")
  206.      */
  207.     private $isDiNews;
  208.     /**
  209.      * Article constructor.
  210.      */
  211.     public function __construct()
  212.     {
  213.         $this->published true;
  214.         $this->image = new EmbeddedFile();
  215.         $this->userLikes = new ArrayCollection();
  216.         $this->nbLikes 0;
  217.         $this->userComments = new ArrayCollection();
  218.         $this->nbComments 0;
  219.         $this->userShares = new ArrayCollection();
  220.         $this->nbShares 0;
  221.         $this->feedItems = new ArrayCollection();
  222.         $this->highlighted false;
  223.         $this->announcement false;
  224.         $this->benefits = new ArrayCollection();
  225.         $this->socialGroupIssues = new ArrayCollection();
  226.         $this->isDiNews false;
  227.     }
  228.     public function __toString()
  229.     {
  230.         return $this->title;
  231.     }
  232.     public function getId(): ?int
  233.     {
  234.         return $this->id;
  235.     }
  236.     public function getTitle(): ?string
  237.     {
  238.         return $this->title;
  239.     }
  240.     public function setTitle(string $title): self
  241.     {
  242.         $this->title $title;
  243.         return $this;
  244.     }
  245.     public function getAuthor(): ?string
  246.     {
  247.         return $this->author;
  248.     }
  249.     public function setAuthor(?string $author): self
  250.     {
  251.         $this->author $author;
  252.         return $this;
  253.     }
  254.     public function getContent(): ?string
  255.     {
  256.         return $this->content;
  257.     }
  258.     public function setContent(?string $content): self
  259.     {
  260.         $this->content $content;
  261.         return $this;
  262.     }
  263.     public function getExcerpt(): ?string
  264.     {
  265.         return $this->excerpt;
  266.     }
  267.     public function setExcerpt(string $excerpt): self
  268.     {
  269.         $this->excerpt $excerpt;
  270.         return $this;
  271.     }
  272.     public function getSlug(): ?string
  273.     {
  274.         return $this->slug;
  275.     }
  276.     public function setSlug(string $slug): self
  277.     {
  278.         $this->slug $slug;
  279.         return $this;
  280.     }
  281.     public function getPublished(): ?bool
  282.     {
  283.         return $this->published;
  284.     }
  285.     public function setPublished(bool $published): self
  286.     {
  287.         $this->published $published;
  288.         return $this;
  289.     }
  290.     public function getExternalLink(): ?string
  291.     {
  292.         return $this->externalLink;
  293.     }
  294.     public function setExternalLink(?string $externalLink): self
  295.     {
  296.         $this->externalLink $externalLink;
  297.         return $this;
  298.     }
  299.     public function getType(): ?string
  300.     {
  301.         return $this->type;
  302.     }
  303.     public function setType(string $type): self
  304.     {
  305.         $this->type $type;
  306.         return $this;
  307.     }
  308.     public function getCompany(): ?Company
  309.     {
  310.         return $this->company;
  311.     }
  312.     public function setCompany(?Company $company): self
  313.     {
  314.         $this->company $company;
  315.         return $this;
  316.     }
  317.     public function getImageFile(): ?File
  318.     {
  319.         return $this->imageFile;
  320.     }
  321.     /**
  322.      * @throws Exception
  323.      */
  324.     public function setImageFile(?File $image): self
  325.     {
  326.         $this->imageFile $image;
  327.         if ($image) {
  328.             $this->updatedAt = new DateTime();
  329.         }
  330.         return $this;
  331.     }
  332.     public function getImage(): EmbeddedFile
  333.     {
  334.         return $this->image;
  335.     }
  336.     public function setImage(EmbeddedFile $image): self
  337.     {
  338.         $this->image $image;
  339.         return $this;
  340.     }
  341.     public function setImageAlt(?string $imageAlt): self
  342.     {
  343.         $this->imageAlt $imageAlt;
  344.         return $this;
  345.     }
  346.     public function getImageAlt(): ?string
  347.     {
  348.         return $this->imageAlt;
  349.     }
  350.     /**
  351.      * @return Collection|UserLike[]
  352.      */
  353.     public function getUserLikes(): Collection
  354.     {
  355.         return $this->userLikes;
  356.     }
  357.     public function addUserLike(UserLike $userLike): self
  358.     {
  359.         if (!$this->userLikes->contains($userLike)) {
  360.             $this->userLikes[] = $userLike;
  361.             $userLike->setArticle($this);
  362.         }
  363.         return $this;
  364.     }
  365.     public function removeUserLike(UserLike $userLike): self
  366.     {
  367.         if ($this->userLikes->contains($userLike)) {
  368.             $this->userLikes->removeElement($userLike);
  369.             // set the owning side to null (unless already changed)
  370.             if ($userLike->getArticle() === $this) {
  371.                 $userLike->setArticle(null);
  372.             }
  373.         }
  374.         return $this;
  375.     }
  376.     public function getNbLikes(): ?int
  377.     {
  378.         return $this->nbLikes;
  379.     }
  380.     public function setNbLikes(int $nbLikes): self
  381.     {
  382.         $this->nbLikes $nbLikes;
  383.         return $this;
  384.     }
  385.     /**
  386.      * @return Collection|UserComment[]
  387.      */
  388.     public function getUserComments(): Collection
  389.     {
  390.         return $this->userComments;
  391.     }
  392.     public function addUserComment(UserComment $userComment): self
  393.     {
  394.         if (!$this->userComments->contains($userComment)) {
  395.             $this->userComments[] = $userComment;
  396.             $userComment->setArticle($this);
  397.         }
  398.         return $this;
  399.     }
  400.     public function removeUserComment(UserComment $userComment): self
  401.     {
  402.         if ($this->userComments->contains($userComment)) {
  403.             $this->userComments->removeElement($userComment);
  404.             // set the owning side to null (unless already changed)
  405.             if ($userComment->getArticle() === $this) {
  406.                 $userComment->setArticle(null);
  407.             }
  408.         }
  409.         return $this;
  410.     }
  411.     public function getNbComments(): int
  412.     {
  413.         return $this->nbComments;
  414.     }
  415.     public function setNbComments(int $nbComments): self
  416.     {
  417.         $this->nbComments $nbComments;
  418.         return $this;
  419.     }
  420.     /**
  421.      * @return Collection|UserShare[]
  422.      */
  423.     public function getUserShares(): Collection
  424.     {
  425.         return $this->userShares;
  426.     }
  427.     public function addUserShare(UserShare $userShare): self
  428.     {
  429.         if (!$this->userShares->contains($userShare)) {
  430.             $this->userShares[] = $userShare;
  431.             $userShare->setArticle($this);
  432.         }
  433.         return $this;
  434.     }
  435.     public function removeUserShare(UserShare $userShare): self
  436.     {
  437.         if ($this->userShares->contains($userShare)) {
  438.             $this->userShares->removeElement($userShare);
  439.             // set the owning side to null (unless already changed)
  440.             if ($userShare->getArticle() === $this) {
  441.                 $userShare->setArticle(null);
  442.             }
  443.         }
  444.         return $this;
  445.     }
  446.     public function getNbShares(): ?int
  447.     {
  448.         return $this->nbShares;
  449.     }
  450.     public function setNbShares(int $nbShares): self
  451.     {
  452.         $this->nbShares $nbShares;
  453.         return $this;
  454.     }
  455.     /**
  456.      * @return Collection|FeedItem[]
  457.      */
  458.     public function getFeedItems(): Collection
  459.     {
  460.         return $this->feedItems;
  461.     }
  462.     public function addFeedItem(FeedItem $feedItem): self
  463.     {
  464.         if (!$this->feedItems->contains($feedItem)) {
  465.             $this->feedItems[] = $feedItem;
  466.             $feedItem->setArticle($this);
  467.         }
  468.         return $this;
  469.     }
  470.     public function removeFeedItem(FeedItem $feedItem): self
  471.     {
  472.         if ($this->feedItems->contains($feedItem)) {
  473.             $this->feedItems->removeElement($feedItem);
  474.             // set the owning side to null (unless already changed)
  475.             if ($feedItem->getArticle() === $this) {
  476.                 $feedItem->setArticle(null);
  477.             }
  478.         }
  479.         return $this;
  480.     }
  481.     public function getHighlighted(): ?bool
  482.     {
  483.         return $this->highlighted;
  484.     }
  485.     public function setHighlighted(bool $highlighted): self
  486.     {
  487.         $this->highlighted $highlighted;
  488.         return $this;
  489.     }
  490.     public function getAnnouncement(): ?bool
  491.     {
  492.         return $this->announcement;
  493.     }
  494.     public function setAnnouncement(bool $announcement): self
  495.     {
  496.         $this->announcement $announcement;
  497.         return $this;
  498.     }
  499.     /**
  500.      * @return Collection|Benefit[]
  501.      */
  502.     public function getBenefits(): Collection
  503.     {
  504.         return $this->benefits;
  505.     }
  506.     public function addBenefit(Benefit $benefit): self
  507.     {
  508.         if (!$this->benefits->contains($benefit)) {
  509.             $this->benefits[] = $benefit;
  510.         }
  511.         return $this;
  512.     }
  513.     public function removeBenefit(Benefit $benefit): self
  514.     {
  515.         if ($this->benefits->contains($benefit)) {
  516.             $this->benefits->removeElement($benefit);
  517.         }
  518.         return $this;
  519.     }
  520.     /**
  521.      * @return Collection|SocialGroupIssue[]
  522.      */
  523.     public function getSocialGroupIssues(): Collection
  524.     {
  525.         return $this->socialGroupIssues;
  526.     }
  527.     public function addSocialGroupIssue(SocialGroupIssue $socialGroupIssue): self
  528.     {
  529.         if (!$this->socialGroupIssues->contains($socialGroupIssue)) {
  530.             $this->socialGroupIssues[] = $socialGroupIssue;
  531.         }
  532.         return $this;
  533.     }
  534.     public function removeSocialGroupIssue(SocialGroupIssue $socialGroupIssue): self
  535.     {
  536.         if ($this->socialGroupIssues->contains($socialGroupIssue)) {
  537.             $this->socialGroupIssues->removeElement($socialGroupIssue);
  538.         }
  539.         return $this;
  540.     }
  541.     public function getIsDiNews(): ?bool
  542.     {
  543.         return $this->isDiNews;
  544.     }
  545.     public function setIsDiNews(bool $isDiNews): self
  546.     {
  547.         $this->isDiNews $isDiNews;
  548.         return $this;
  549.     }
  550. }