<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Blog\GetBlogLast;
use App\Controller\Api\Blog\GetBlogs;
use App\Entity\MediaObject\MediaBlog;
use App\Entity\MediaObject\MediaThumbnailBlog;
use App\Entity\Tag\BlogTag;
use App\Repository\BlogRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* attributes={
* "normalization_context"={
* "groups"={
* "Blog:output",
* "Blog:io",
* },
* },
* "denormalization_context"={
* "groups"={
* "Blog:input",
* "Blog:io",
* },
* }
* },
* collectionOperations={
* "get"={
* "method"="GET",
* "path"="/public/blogs",
* "access_control"="is_granted('ROLE_PUBLIC')",
* "controller"=GetBlogs::class,
* "defaults"={"_api_receive"=false},
* },
* "get-blog-last"={
* "method"="GET",
* "path"="/public/blog-last",
* "access_control"="is_granted('ROLE_PUBLIC')",
* "controller"=GetBlogLast::class,
* "defaults"={"_api_receive"=false},
* }
* },
* itemOperations={
* "get"={
* "method"="GET",
* "path"="/public/blog/{id}",
* }
* }
* )
*
* @ORM\Entity(repositoryClass=BlogRepository::class)
*/
class Blog
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @SerializedName("id")
* @ApiProperty(identifier=true)
* @Groups({
* "Blog:output",
* })
*/
private string $id;
/**
* @ORM\Column(type="string", length=255)
* @SerializedName("title")
* @Groups({
* "Blog:output",
* })
*/
private ?string $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @SerializedName("subtitle")
* @Groups({
* "Blog:output",
* })
*/
private ?string $subtitle;
/**
* @ORM\Column(type="text", nullable=true)
* @SerializedName("text")
* @Groups({
* "Blog:output",
* })
*/
private ?string $text;
/**
* @var MediaBlog|null
* @ORM\OneToMany(targetEntity=MediaBlog::class, mappedBy="blog", cascade={"persist"})
* @SerializedName("media_blogs")
* @Groups({
* "Blog:output",
* "MediaBlog:io"
* })
*/
private $mediaBlogs;
/**
* @var MediaThumbnailBlog|null
* @ORM\OneToMany(targetEntity=MediaThumbnailBlog::class, mappedBy="blog", cascade={"persist"})
* @SerializedName("thumbnail_blogs")
* @Groups({
* "Blog:output",
* "MediaThumbnailBlog:io"
* })
*/
private $thumbnailBlogs;
/**
* @ORM\ManyToMany(targetEntity=BlogTag::class, inversedBy="blogs")
* @SerializedName("tags")
* @Groups({
* "Blog:output",
* })
*/
public Collection $tags;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=true)
* @SerializedName("published_at")
* @Groups({
* "Blog:output",
* })
*/
public $publishedAt;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=true)
* @SerializedName("ended_at")
* @Groups({
* "Blog:output",
* })
*/
public $endedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @SerializedName("youtube_url")
* @Assert\Regex(
* "/http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?/",
* message="Vous devez mettre un lien youtube valide (exemple: 'https://www.youtube.com/watch?v=DXnLfu4ANlA')"
* )
* @Groups({
* "Blog:output",
* })
*/
private ?string $youtubeUrl = null;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->tags = new ArrayCollection();
$this->mediaBlogs = new ArrayCollection();
$this->thumbnailBlogs = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getSubtitle(): ?string
{
return $this->subtitle;
}
public function setSubtitle(?string $subtitle): self
{
$this->subtitle = $subtitle;
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
/**
* @return Collection|BlogTag[]
*/
public function getTags(): ?Collection
{
return $this->tags;
}
public function addTag(BlogTag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
$tag->addBlog($this);
}
return $this;
}
public function removeTag(BlogTag $tag): self
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
$tag->removeBlog($this);
}
return $this;
}
public function getMediaBlogs()
{
return $this->mediaBlogs;
}
public function setMediaBlog(MediaBlog $mediaBlogs): Blog
{
$this->mediaBlogs = $mediaBlogs;
return $this;
}
public function addMediaBlog(MediaBlog $mediaBlog)
{
if ($this->mediaBlogs->contains($mediaBlog)) {
return;
}
$this->mediaBlogs[] = $mediaBlog;
$mediaBlog->setBlog($this);
}
public function __toString()
{
return $this->title;
}
public function getThumbnailBlogs()
{
return $this->thumbnailBlogs;
}
public function setThumbnailBlog(MediaThumbnailBlog $thumbnailBlogs): Blog
{
$this->thumbnailBlogs = $thumbnailBlogs;
return $this;
}
public function addThumbnailBlog(MediaThumbnailBlog $thumbnailBlog)
{
if ($this->thumbnailBlogs->contains($thumbnailBlog)) {
return;
}
$this->thumbnailBlogs[] = $thumbnailBlog;
$thumbnailBlog->setBlog($this);
}
public function getPublishedAt(): ?\DateTime
{
return $this->publishedAt;
}
public function setPublishedAt(?\DateTime $publishedAt): Blog
{
$this->publishedAt = $publishedAt;
return $this;
}
public function getEndedAt(): ?\DateTime
{
return $this->endedAt;
}
public function setEndedAt(?\DateTime $endedAt): Blog
{
$this->endedAt = $endedAt;
return $this;
}
public function getYoutubeUrl(): ?string
{
return $this->youtubeUrl;
}
public function setYoutubeUrl(?string $youtubeUrl): self
{
$this->youtubeUrl = $youtubeUrl;
return $this;
}
}