<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiProperty;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
#[ApiResource(
normalizationContext: ['groups' => ['cat:read']],
denormalizationContext: ['groups' => ['cat:write']],
order: ['id' => 'DESC'],
paginationPartial: true
)]
#[ApiFilter(SearchFilter::class, properties: [
'name' => 'ipartial',
'parent' => 'exact',
'main' => 'exact',
'type' => 'exact',
'slug' => 'exact',
'products.show' => 'exact',
'products.id' => 'exact',
'attributes.id' => 'exact',
'news.id' => 'exact',
'pages.id' => 'exact',
])]
class Category
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['cat:read', 'cat:write', 'attributes:read', 'attributes_items:read', 'coupon:read', 'read', 'write', 'news:read', 'news:write'])]
private ?int $id = null;
#[Groups(['cat:read'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $date_entered = null;
#[Groups(['cat:read'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $date_modified = null;
#[ORM\Column(nullable: true)]
private ?int $created_by = null;
#[ORM\Column(nullable: true)]
private ?int $modified_user_id = null;
#[Groups(['cat:read', 'cat:write', 'attributes:read', 'attributes_items:read', 'coupon:read', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\Column(length: 255)]
private ?string $name = null;
#[Groups(['cat:read', 'cat:write', 'attributes:read', 'attributes_items:read', 'coupon:read', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'categories', cascade:['persist'])]
private ?self $parent = null;
#[Groups(['cat:read'])]
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class, cascade:['persist', 'remove'])]
#[MaxDepth(2)]
#[ApiProperty(writable: true)]
private Collection $categories;
#[Groups(['cat:read', 'cat:write', 'attributes:read', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\Column(length: 100, nullable: true)]
private ?string $slug = null;
#[Groups(['cat:read', 'cat:write', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\Column(length: 100, nullable: true)]
private ?string $status = null;
#[Groups(['cat:read', 'cat:write', 'read', 'write','news:read', 'news:write'])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[Groups(['cat:read', 'cat:write', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\Column(nullable: true)]
private ?bool $main = null;
#[Groups(['cat:read', 'cat:write', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\Column(length: 20, nullable: true)]
private ?string $type = null;
#[ApiProperty(writable: true)]
// #[Groups(['cat:read', 'cat:write'])]
#[ORM\ManyToMany(targetEntity: Products::class, mappedBy: 'category', cascade:['persist'])]
#[MaxDepth(1)]
private Collection $products;
#[Groups(['cat:read', 'cat:write'])]
#[ORM\ManyToMany(targetEntity: Attributes::class, mappedBy: 'category', cascade:['persist'])]
private Collection $attributes;
#[ORM\ManyToMany(targetEntity: AttributeItems::class, mappedBy: 'categories', cascade:['persist'])]
private Collection $attributeItems;
#[ORM\ManyToMany(targetEntity: Coupons::class, mappedBy: 'category', cascade:['persist'])]
private Collection $coupons;
#[Groups(['cat:read', 'cat:write'])]
#[ORM\ManyToMany(targetEntity: Pages::class, mappedBy: 'categories', cascade:['persist'])]
private Collection $pages;
#[Groups(['cat:read', 'cat:write'])]
#[ORM\ManyToMany(targetEntity: News::class, inversedBy: 'categories', cascade:['persist'])]
private Collection $news;
#[Groups(['cat:read', 'cat:write', 'attributes:read', 'read', 'write', 'news:read', 'news:write'])]
#[ORM\Column(length: 100, nullable: true)]
private ?string $code1c = null;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->products = new ArrayCollection();
$this->attributes = new ArrayCollection();
$this->attributeItems = new ArrayCollection();
$this->coupons = new ArrayCollection();
$this->pages = new ArrayCollection();
$this->news = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getDateEntered(): ?\DateTimeInterface
{
return $this->date_entered;
}
public function setDateEntered(?\DateTimeInterface $date_entered): self
{
$this->date_entered = $date_entered;
return $this;
}
public function getDateModified(): ?\DateTimeInterface
{
return $this->date_modified;
}
public function setDateModified(?\DateTimeInterface $date_modified): self
{
$this->date_modified = $date_modified;
return $this;
}
public function getCreatedBy(): ?int
{
return $this->created_by;
}
public function setCreatedBy(?int $created_by): self
{
$this->created_by = $created_by;
return $this;
}
public function getModifiedUserId(): ?int
{
return $this->modified_user_id;
}
public function setModifiedUserId(?int $modified_user_id): self
{
$this->modified_user_id = $modified_user_id;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(self $category): self
{
if (!$this->categories->contains($category)) {
$this->categories->add($category);
$category->setParent($this);
}
return $this;
}
public function removeCategory(self $category): self
{
if ($this->categories->removeElement($category)) {
// set the owning side to null (unless already changed)
if ($category->getParent() === $this) {
$category->setParent(null);
}
}
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): self
{
$this->slug = mb_strtolower( $slug );
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): self
{
$this->status = $status;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function isMain(): ?bool
{
return $this->main;
}
public function setMain(?bool $main): self
{
$this->main = $main;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection<int, Products>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Products $product): self
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->addCategory($this);
}
return $this;
}
public function removeProduct(Products $product): self
{
if ($this->products->removeElement($product)) {
$product->removeCategory($this);
}
return $this;
}
/**
* @return Collection<int, Attributes>
*/
public function getAttributes(): Collection
{
return $this->attributes;
}
public function addAttribute(Attributes $attribute): self
{
if (!$this->attributes->contains($attribute)) {
$this->attributes->add($attribute);
$attribute->addCategory($this);
}
return $this;
}
public function removeAttribute(Attributes $attribute): self
{
if ($this->attributes->removeElement($attribute)) {
$attribute->removeCategory($this);
}
return $this;
}
/**
* @return Collection<int, AttributeItems>
*/
public function getAttributeItems(): Collection
{
return $this->attributeItems;
}
public function addAttributeItem(AttributeItems $attributeItem): self
{
if (!$this->attributeItems->contains($attributeItem)) {
$this->attributeItems->add($attributeItem);
$attributeItem->addCategory($this);
}
return $this;
}
public function removeAttributeItem(AttributeItems $attributeItem): self
{
if ($this->attributeItems->removeElement($attributeItem)) {
$attributeItem->removeCategory($this);
}
return $this;
}
/**
* @return Collection<int, Coupons>
*/
public function getCoupons(): Collection
{
return $this->coupons;
}
public function addCoupon(Coupons $coupon): self
{
if (!$this->coupons->contains($coupon)) {
$this->coupons->add($coupon);
$coupon->addCategory($this);
}
return $this;
}
public function removeCoupon(Coupons $coupon): self
{
if ($this->coupons->removeElement($coupon)) {
$coupon->removeCategory($this);
}
return $this;
}
/**
* @return Collection<int, Pages>
*/
public function getPages(): Collection
{
return $this->pages;
}
public function addPage(Pages $page): static
{
if (!$this->pages->contains($page)) {
$this->pages->add($page);
}
return $this;
}
public function removePage(Pages $page): static
{
$this->pages->removeElement($page);
return $this;
}
/**
* @return Collection<int, News>
*/
public function getNews(): Collection
{
return $this->news;
}
public function addNews(News $news): static
{
if (!$this->news->contains($news)) {
$this->news->add($news);
}
return $this;
}
public function removeNews(News $news): static
{
$this->news->removeElement($news);
return $this;
}
public function getCode1c(): ?string
{
return $this->code1c;
}
public function setCode1c(?string $code1c): static
{
$this->code1c = $code1c;
return $this;
}
}