src/Entity/Blog.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Controller\Api\Blog\GetBlogLast;
  6. use App\Controller\Api\Blog\GetBlogs;
  7. use App\Entity\MediaObject\MediaBlog;
  8. use App\Entity\MediaObject\MediaThumbnailBlog;
  9. use App\Entity\Tag\BlogTag;
  10. use App\Repository\BlogRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Timestampable\Traits\TimestampableEntity;
  15. use Ramsey\Uuid\Doctrine\UuidGenerator;
  16. use Ramsey\Uuid\Uuid;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Serializer\Annotation\SerializedName;
  19. use Symfony\Component\Validator\Constraints as Assert;
  20. /**
  21.  * @ApiResource(
  22.  *     attributes={
  23.  *          "normalization_context"={
  24.  *              "groups"={
  25.  *                  "Blog:output",
  26.  *                  "Blog:io",
  27.  *              },
  28.  *          },
  29.  *          "denormalization_context"={
  30.  *              "groups"={
  31.  *                  "Blog:input",
  32.  *                  "Blog:io",
  33.  *              },
  34.  *          }
  35.  *      },
  36.  *     collectionOperations={
  37.  *          "get"={
  38.  *              "method"="GET",
  39.  *              "path"="/public/blogs",
  40.  *              "access_control"="is_granted('ROLE_PUBLIC')",
  41.  *              "controller"=GetBlogs::class,
  42.  *              "defaults"={"_api_receive"=false},
  43.  *          },
  44.  *          "get-blog-last"={
  45.  *              "method"="GET",
  46.  *              "path"="/public/blog-last",
  47.  *              "access_control"="is_granted('ROLE_PUBLIC')",
  48.  *              "controller"=GetBlogLast::class,
  49.  *              "defaults"={"_api_receive"=false},
  50.  *          }
  51.  *     },
  52.  *     itemOperations={
  53.  *          "get"={
  54.  *              "method"="GET",
  55.  *              "path"="/public/blog/{id}",
  56.  *          }
  57.  *     }
  58.  * )
  59.  *
  60.  * @ORM\Entity(repositoryClass=BlogRepository::class)
  61.  */
  62. class Blog
  63. {
  64.     /**
  65.      * Hook timestampable behavior
  66.      * updates createdAt, updatedAt fields
  67.      */
  68.     use TimestampableEntity;
  69.     /**
  70.      * @ORM\Id
  71.      * @ORM\Column(type="uuid", unique=true)
  72.      * @ORM\GeneratedValue(strategy="CUSTOM")
  73.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  74.      * @SerializedName("id")
  75.      * @ApiProperty(identifier=true)
  76.      * @Groups({
  77.      *     "Blog:output",
  78.      * })
  79.      */
  80.     private string $id;
  81.     /**
  82.      * @ORM\Column(type="string", length=255)
  83.      * @SerializedName("title")
  84.      * @Groups({
  85.      *     "Blog:output",
  86.      * })
  87.      */
  88.     private ?string $title;
  89.     /**
  90.      * @ORM\Column(type="string", length=255, nullable=true)
  91.      * @SerializedName("subtitle")
  92.      * @Groups({
  93.      *     "Blog:output",
  94.      * })
  95.      */
  96.     private ?string $subtitle;
  97.     /**
  98.      * @ORM\Column(type="text", nullable=true)
  99.      * @SerializedName("text")
  100.      * @Groups({
  101.      *     "Blog:output",
  102.      * })
  103.      */
  104.     private ?string $text;
  105.     /**
  106.      * @var MediaBlog|null
  107.      * @ORM\OneToMany(targetEntity=MediaBlog::class, mappedBy="blog", cascade={"persist"})
  108.      * @SerializedName("media_blogs")
  109.      * @Groups({
  110.      *     "Blog:output",
  111.      *     "MediaBlog:io"
  112.      * })
  113.      */
  114.     private $mediaBlogs;
  115.     /**
  116.      * @var MediaThumbnailBlog|null
  117.      * @ORM\OneToMany(targetEntity=MediaThumbnailBlog::class, mappedBy="blog", cascade={"persist"})
  118.      * @SerializedName("thumbnail_blogs")
  119.      * @Groups({
  120.      *     "Blog:output",
  121.      *     "MediaThumbnailBlog:io"
  122.      * })
  123.      */
  124.     private $thumbnailBlogs;
  125.     /**
  126.      * @ORM\ManyToMany(targetEntity=BlogTag::class, inversedBy="blogs")
  127.      * @SerializedName("tags")
  128.      * @Groups({
  129.      *     "Blog:output",
  130.      * })
  131.      */
  132.     public Collection $tags;
  133.     /**
  134.      * @var \DateTime
  135.      * @ORM\Column(type="datetime", nullable=true)
  136.      * @SerializedName("published_at")
  137.      * @Groups({
  138.      *     "Blog:output",
  139.      * })
  140.      */
  141.     public $publishedAt;
  142.     /**
  143.      * @var \DateTime
  144.      * @ORM\Column(type="datetime", nullable=true)
  145.      * @SerializedName("ended_at")
  146.      * @Groups({
  147.      *     "Blog:output",
  148.      * })
  149.      */
  150.     public $endedAt;
  151.     /**
  152.      * @ORM\Column(type="string", length=255, nullable=true)
  153.      * @SerializedName("youtube_url")
  154.      * @Assert\Regex(
  155.      *     "/http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/",
  156.      *     message="Vous devez mettre un lien youtube valide (exemple: 'https://www.youtube.com/watch?v=DXnLfu4ANlA')"
  157.      * )
  158.      * @Groups({
  159.      *     "Blog:output",
  160.      * })
  161.      */
  162.     private ?string $youtubeUrl null;
  163.     public function __construct()
  164.     {
  165.         $this->id Uuid::uuid4();
  166.         $this->tags = new ArrayCollection();
  167.         $this->mediaBlogs = new ArrayCollection();
  168.         $this->thumbnailBlogs = new ArrayCollection();
  169.     }
  170.     public function getId(): ?string
  171.     {
  172.         return $this->id;
  173.     }
  174.     public function getTitle(): ?string
  175.     {
  176.         return $this->title;
  177.     }
  178.     public function setTitle(string $title): self
  179.     {
  180.         $this->title $title;
  181.         return $this;
  182.     }
  183.     public function getSubtitle(): ?string
  184.     {
  185.         return $this->subtitle;
  186.     }
  187.     public function setSubtitle(?string $subtitle): self
  188.     {
  189.         $this->subtitle $subtitle;
  190.         return $this;
  191.     }
  192.     public function getText(): ?string
  193.     {
  194.         return $this->text;
  195.     }
  196.     public function setText(?string $text): self
  197.     {
  198.         $this->text $text;
  199.         return $this;
  200.     }
  201.     /**
  202.      * @return Collection|BlogTag[]
  203.      */
  204.     public function getTags(): ?Collection
  205.     {
  206.         return $this->tags;
  207.     }
  208.     public function addTag(BlogTag $tag): self
  209.     {
  210.         if (!$this->tags->contains($tag)) {
  211.             $this->tags[] = $tag;
  212.             $tag->addBlog($this);
  213.         }
  214.         return $this;
  215.     }
  216.     public function removeTag(BlogTag $tag): self
  217.     {
  218.         if ($this->tags->contains($tag)) {
  219.             $this->tags->removeElement($tag);
  220.             $tag->removeBlog($this);
  221.         }
  222.         return $this;
  223.     }
  224.     public function getMediaBlogs()
  225.     {
  226.         return $this->mediaBlogs;
  227.     }
  228.     public function setMediaBlog(MediaBlog $mediaBlogs): Blog
  229.     {
  230.         $this->mediaBlogs $mediaBlogs;
  231.         return $this;
  232.     }
  233.     public function addMediaBlog(MediaBlog $mediaBlog)
  234.     {
  235.         if ($this->mediaBlogs->contains($mediaBlog)) {
  236.             return;
  237.         }
  238.         $this->mediaBlogs[] = $mediaBlog;
  239.         $mediaBlog->setBlog($this);
  240.     }
  241.     public function __toString()
  242.     {
  243.         return $this->title;
  244.     }
  245.     public function getThumbnailBlogs()
  246.     {
  247.         return $this->thumbnailBlogs;
  248.     }
  249.     public function setThumbnailBlog(MediaThumbnailBlog $thumbnailBlogs): Blog
  250.     {
  251.         $this->thumbnailBlogs $thumbnailBlogs;
  252.         return $this;
  253.     }
  254.     public function addThumbnailBlog(MediaThumbnailBlog $thumbnailBlog)
  255.     {
  256.         if ($this->thumbnailBlogs->contains($thumbnailBlog)) {
  257.             return;
  258.         }
  259.         $this->thumbnailBlogs[] = $thumbnailBlog;
  260.         $thumbnailBlog->setBlog($this);
  261.     }
  262.     public function getPublishedAt(): ?\DateTime
  263.     {
  264.         return $this->publishedAt;
  265.     }
  266.     public function setPublishedAt(?\DateTime $publishedAt): Blog
  267.     {
  268.         $this->publishedAt $publishedAt;
  269.         return $this;
  270.     }
  271.     public function getEndedAt(): ?\DateTime
  272.     {
  273.         return $this->endedAt;
  274.     }
  275.     public function setEndedAt(?\DateTime $endedAt): Blog
  276.     {
  277.         $this->endedAt $endedAt;
  278.         return $this;
  279.     }
  280.     public function getYoutubeUrl(): ?string
  281.     {
  282.         return $this->youtubeUrl;
  283.     }
  284.     public function setYoutubeUrl(?string $youtubeUrl): self
  285.     {
  286.         $this->youtubeUrl $youtubeUrl;
  287.         return $this;
  288.     }
  289. }