<?php
namespace App\Entity\Tag;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Blog;
use App\Repository\Tag\BlogTagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ORM\Entity(repositoryClass=BlogTagRepository::class)
*
* @ApiResource(
* attributes={
* "order"={"title": "ASC"},
* "normalization_context"={"groups"={
* "BlogTag:output",
* "BlogTag:io",
* "Blog:output",
* }},
* "denormalization_context"={"groups"={
* "BlogTag:input",
* "BlogTag:io",
* }}
* },
* collectionOperations={
* "get"={
* "method"="GET",
* "path"="/public/blog-tags",
* }
* },
* itemOperations={
* "get"={
* "method"="GET",
* "path"="/public/blog-tag/{id}",
* }
* },
* )
*/
class BlogTag extends Tag
{
/**
* @ORM\ManyToMany(targetEntity=Blog::class, mappedBy="tags")
* @SerializedName("blogs")
*/
public Collection $blogs;
public function __construct()
{
$this->blogs = new ArrayCollection();
}
/**
* @return Collection|Blog[]
*/
public function getBlogs(): ?Collection
{
return $this->blogs;
}
public function addBlog(Blog $blog): self
{
if (!$this->blogs->contains($blog)) {
$this->blogs[] = $blog;
$blog->addTag($this);
}
return $this;
}
public function removeBlog(Blog $blog): self
{
if ($this->blogs->contains($blog)) {
$this->blogs->removeElement($blog);
$blog->removeTag($this);
}
return $this;
}
public function __toString(): string
{
return $this->getTitle();
}
public function getBlogsListToHtml()
{
$html = '';
/** @var Blog $blog */
foreach ($this->blogs->toArray() as $blog) {
$html .= '<li>'.$blog->getTitle().'</li>';
}
return '<ul>'.$html.'</ul>';
}
}