<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Delete;
use App\Dto\CategoryOutput;
use App\State\CategoryRepresentationProvider;
use App\State\CategoriesRepresentationProvider;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ApiResource(
normalizationContext: ['groups' => ['category:read'], 'enable_max_depth' => 'true'],
denormalizationContext: ['groups' => ['category:write'], 'enable_max_depth' => 'true'],
paginationItemsPerPage: 500,
operations: [
new Get(),
new GetCollection(),
new Put(security: "is_granted('ROLE_USER')",),
new Post(security: "is_granted('ROLE_USER')",),
new Patch(security: "is_granted('ROLE_USER')",),
new Delete(security: "is_granted('ROLE_USER')",),
new GetCollection(
uriTemplate: "/categoriesRepresentation",
output: CategoryOutput::class,
provider: CategoriesRepresentationProvider::class,
),
new Get(
uriTemplate: "/categoryRepresentation/{slug}",
output: CategoryOutput::class,
provider: CategoryRepresentationProvider::class,
),
]
)]
#[Gedmo\Tree(type: "nested")]
#[ApiFilter(OrderFilter::class)]
#[ApiFilter(DateFilter::class, properties: ["createdAt"])]
#[ApiFilter(SearchFilter::class)]
#[ApiFilter(SearchFilter::class, properties: ["name" => "partial"])]
#[ApiFilter(BooleanFilter::class, properties: ["public" => "exact"])]
#[UniqueEntity('slug')]
#[ORM\Entity(repositoryClass: \Gedmo\Tree\Entity\Repository\NestedTreeRepository::class)]
class Category implements DateCmsInterface, AuthorCmsInterface
{
#[ApiProperty(identifier: false)]
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[Groups(['category:read'])]
private ?int $id = null;
#[ApiProperty(types: ["https://schema.org/name"])]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 255)]
#[Groups(['category:read', 'category:write'])]
private ?string $name = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 1024, nullable: true)]
#[Groups(['category:read', 'category:write'])]
private ?string $short = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN)]
#[Groups(['category:read', 'category:write'])]
private ?bool $public = true;
#[Gedmo\Slug(fields: ["name"])]
#[ApiProperty(identifier: true)]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, nullable: true, length: 80, unique: true)]
#[Groups(['category:read'])]
private ?string $slug = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[Groups(['category:read', 'category:write'])]
private ?\App\Entity\User $author = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $createdAt = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $updatedAt = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[Groups(['category:read', 'category:write'])]
private ?int $type = null;
/**
* @var \Doctrine\Common\Collections\Collection<\App\Entity\Content>
*/
#[ORM\ManyToMany(targetEntity: Content::class, mappedBy: 'category')]
#[Groups(['category:read'])]
private \Doctrine\Common\Collections\Collection $contents;
#[Gedmo\TreeLeft]
#[ORM\Column(name: 'lft', type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[Groups(['category:read', 'category:write'])]
private ?int $lft = null;
#[Gedmo\TreeLevel]
#[ORM\Column(name: 'lvl', type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[Groups(['category:read', 'category:write'])]
private ?int $lvl = null;
#[Gedmo\TreeRight]
#[ORM\Column(name: 'rgt', type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[Groups(['category:read', 'category:write'])]
private ?int $rgt = null;
#[Gedmo\TreeRoot]
#[ORM\ManyToOne(targetEntity: 'Category')]
#[ORM\JoinColumn(name: 'tree_root', onDelete: 'CASCADE')]
#[Groups(['category:read', 'category:write'])]
private ?\App\Entity\Category $root = null;
##[MaxDepth(1)]
#[ApiFilter(SearchFilter::class)]
#[Gedmo\TreeParent]
#[ORM\ManyToOne(targetEntity: 'Category', inversedBy: 'children')]
#[ORM\JoinColumn(name: 'parent_id', onDelete: 'CASCADE')]
#[Groups(['category:read', 'category:write'])]
#[ApiProperty(readableLink: false, writableLink: false)]
private ?\App\Entity\Category $parent = null;
/**
* @var string|null
*/
#[Groups(['category:read'])]
public $parentName;
/**
* @var \Doctrine\Common\Collections\Collection<\App\Entity\Category>
*/
##[MaxDepth(1)]
#[ORM\OneToMany(targetEntity: 'Category', mappedBy: 'parent')]
#[ORM\OrderBy(['lft' => 'ASC'])]
#[Groups(['category:read', 'category:write'])]
private \Doctrine\Common\Collections\Collection $children;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: true)]
#[Groups(['category:read', 'category:write'])]
private ?int $position = null;
/**
* @var MediaObject|null
*
*/
#[ApiProperty(types: ["https://schema.org/image"])]
#[ORM\ManyToOne(targetEntity: MediaObject::class)]
#[ORM\JoinColumn]
#[Groups(['category:read', 'category:write'])]
private ?\App\Entity\MediaObject $image = null;
#[ApiFilter(BooleanFilter::class)]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN)]
#[Groups(['category:read', 'category:write'])]
private ?bool $showInFooter = false;
/**
* @var \Doctrine\Common\Collections\Collection<\App\Entity\Product>
*/
#[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'category')]
private \Doctrine\Common\Collections\Collection $products;
public function __construct()
{
$this->children = new ArrayCollection();
$this->contents = new ArrayCollection();
$this->products = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getShort(): ?string
{
return $this->short;
}
public function setShort(?string $short): self
{
$this->short = $short;
return $this;
}
public function getPublic(): ?bool
{
return $this->public;
}
public function setPublic(?bool $public): self
{
$this->public = $public;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(?int $position): self
{
$this->position = $position;
return $this;
}
public function getImage(): ?MediaObject
{
return $this->image;
}
public function setImage(?MediaObject $image): self
{
$this->image = $image;
return $this;
}
public function getLft(): ?int
{
return $this->lft;
}
public function setLft(int $lft): self
{
$this->lft = $lft;
return $this;
}
public function getLvl(): ?int
{
return $this->lvl;
}
public function setLvl(int $lvl): self
{
$this->lvl = $lvl;
return $this;
}
public function getRgt(): ?int
{
return $this->rgt;
}
public function setRgt(int $rgt): self
{
$this->rgt = $rgt;
return $this;
}
public function getRoot(): ?self
{
return $this->root;
}
public function setRoot(?self $root): self
{
$this->root = $root;
return $this;
}
/**
* @return Collection|Category[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(Category $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(Category $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function getShowInFooter(): ?bool
{
return $this->showInFooter;
}
public function setShowInFooter(?bool $showInFooter): self
{
$this->showInFooter = $showInFooter;
return $this;
}
/**
* @return Collection|Content[]
*/
public function getContents(): Collection
{
return $this->contents;
}
public function addContent(Content $content): self
{
if (!$this->contents->contains($content)) {
$this->contents[] = $content;
$content->addCategory($this);
}
return $this;
}
public function removeContent(Content $content): self
{
if ($this->contents->removeElement($content)) {
$content->removeCategory($this);
}
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->addCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
$product->removeCategory($this);
}
return $this;
}
}