<?php
namespace App\Model\Category;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Model\IriNormalizableInterface;
use App\Model\NormalizeAsIRITrait;
use App\Model\Product\Product;
use App\Model\Traits\BlamableTrait;
use App\Model\Traits\ImportableObjectTrait;
use App\Model\Traits\TimestampableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
/**
* @ApiResource(
* attributes={
* "denormalization_context": {
* "api_allow_update": true
* }
* },
* collectionOperations={
* "get": {
* "normalization_context": {
* "groups": {"category:list"},
* "enable_max_depth": true
* },
* "security": "is_granted(constant('App\\Voters\\ProductVoter::CATEGORY_ADD_EDIT'), 'App\Voters\ProductVoter')"
* },
* "put_save": {
* "normalization_context": {
* "groups": {"category:read"},
* "enable_max_depth": false
* },
* "denormalization_context": {
* "groups": {"category:save"}
* },
* "method": "PUT",
* "path": "/categories/service/save",
* "controller": "App\Controller\Category\CategoryController",
* "security": "is_granted(constant('App\\Voters\\ProductVoter::CATEGORY_ADD_EDIT'), 'App\Voters\ProductVoter')"
* },
* },
* itemOperations={
* },
* paginationClientEnabled=true,
* paginationClientItemsPerPage=true
* )
*/
class Category implements IriNormalizableInterface
{
use BlamableTrait;
use TimestampableTrait;
use ImportableObjectTrait;
use NormalizeAsIRITrait;
/**
* @ApiProperty(identifier=true)
*
* @var string|null
*
* @Groups({
* "list",
* "product:list", "product:read", "product:write", "product:update",
* "category:list", "category:read", "category:write", "category:save"
* })
*/
private $id;
/**
* @var string|null
*
* @Groups({
* "product:list", "product:read",
* "category:list", "category:read"
* })
*/
private $externalId;
/**
* @var string|null
*
* @Groups({
* "product:list", "product:read",
* "category:list", "category:read", "category:save"
* })
*/
private $customerId;
/**
* @var Category|null
* @Groups({
* "list", "read",
* "category:list", "category:update", "category:save"
* })
* @MaxDepth(1)
*/
private $parent;
/**
* @var Category[]|ArrayCollection
* @Groups({
* "list", "read",
* "category:list"
* })
*/
private $children;
/**
* @var string|null
*
* @Groups({
* "list",
* "product:list", "product:read",
* "category:list", "category:read", "category:update", "category:save"
* })
*/
private $name;
/**
* @var int|null
*
* @Groups({
* "product:list", "product:read",
* "category:list", "category:read", "category:update", "category:save"
* })
*/
private $order;
/**
* @var Product[]|Collection
*/
private $products;
public function __construct()
{
$this->children = new ArrayCollection();
$this->products = new ArrayCollection();
}
/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}
/**
* @param string $id
*
* @return self
*/
public function setId(string $id): self
{
$this->id = $id;
return $this;
}
/**
* @return string|null
*/
public function getCustomerId(): ?string
{
return $this->customerId;
}
/**
* @param string $customerId
*
* @return self
*/
public function setCustomerId(string $customerId): self
{
$this->customerId = $customerId;
return $this;
}
/**
* @return string|null
*/
public function getExternalId(): ?string
{
return $this->externalId;
}
/**
* @param string|null $externalId
*
* @return self
*/
public function setExternalId(?string $externalId): self
{
$this->externalId = $externalId;
return $this;
}
/**
* @return self|null
*/
public function getParent(): ?Category
{
return $this->parent;
}
/**
* @param Category|null $parent
*
* @return self
*/
public function setParent(?Category $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @deprecated Use addChild() instead.
* This is stupid, but the ImportableObjectTrait checks for adders using add.ucfirst($type) where $type is the class name.
*
* @param Category $category
*
* @return $this
*/
public function addCategory(Category $category): self
{
return $this->addChild($category);
}
/**
* @param Category $category
*
* @return $this
*/
public function addChild(Category $category): self
{
if (!$this->children->contains($category)) {
$this->children->add($category);
$category->setParent($this);
}
return $this;
}
/**
* @return Category[]|Collection
*/
public function getChildren(): Collection
{
return $this->children;
}
/**
* @param Category[]|Collection $children
*
* @return self
*/
public function setChildren(Collection $children): self
{
$this->children = $children;
return $this;
}
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
*
* @return self
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return int|null
*/
public function getOrder(): ?int
{
return $this->order;
}
/**
* @param int|null $order
*
* @return self
*/
public function setOrder(?int $order): self
{
$this->order = $order;
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProduct(): Collection
{
return $this->products;
}
/**
* @param Collection|Product[] $products
*
* @return $this
*/
public function setProduct(Collection $products): self
{
$this->products = $products;
return $this;
}
/**
* @param Product $product
*
* @return $this
*/
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->addCategory($this);
}
return $this;
}
/**
* @param Product $product
*
* @return $this
*/
public function removeProduct(Product $product): self
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
$product->removeCategory($this);
}
return $this;
}
}