src/Model/Category/Category.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Model\Category;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Model\IriNormalizableInterface;
  6. use App\Model\NormalizeAsIRITrait;
  7. use App\Model\Product\Product;
  8. use App\Model\Traits\BlamableTrait;
  9. use App\Model\Traits\ImportableObjectTrait;
  10. use App\Model\Traits\TimestampableTrait;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Serializer\Annotation\MaxDepth;
  15. /**
  16.  * @ApiResource(
  17.  *     attributes={
  18.  *         "denormalization_context": {
  19.  *             "api_allow_update": true
  20.  *         }
  21.  *     },
  22.  *     collectionOperations={
  23.  *         "get": {
  24.  *             "normalization_context": {
  25.  *                 "groups": {"category:list"},
  26.  *                 "enable_max_depth": true
  27.  *             },
  28.  *             "security": "is_granted(constant('App\\Voters\\ProductVoter::CATEGORY_ADD_EDIT'), 'App\Voters\ProductVoter')"
  29.  *         },
  30.  *         "put_save": {
  31.  *             "normalization_context": {
  32.  *                 "groups": {"category:read"},
  33.  *                 "enable_max_depth": false
  34.  *             },
  35.  *             "denormalization_context": {
  36.  *                 "groups": {"category:save"}
  37.  *             },
  38.  *             "method": "PUT",
  39.  *             "path": "/categories/service/save",
  40.  *             "controller": "App\Controller\Category\CategoryController",
  41.  *             "security": "is_granted(constant('App\\Voters\\ProductVoter::CATEGORY_ADD_EDIT'), 'App\Voters\ProductVoter')"
  42.  *         },
  43.  *     },
  44.  *     itemOperations={
  45.  *     },
  46.  *     paginationClientEnabled=true,
  47.  *     paginationClientItemsPerPage=true
  48.  * )
  49.  */
  50. class Category implements IriNormalizableInterface
  51. {
  52.     use BlamableTrait;
  53.     use TimestampableTrait;
  54.     use ImportableObjectTrait;
  55.     use NormalizeAsIRITrait;
  56.     /**
  57.      * @ApiProperty(identifier=true)
  58.      *
  59.      * @var string|null
  60.      *
  61.      * @Groups({
  62.      *     "list",
  63.      *     "product:list", "product:read", "product:write", "product:update",
  64.      *     "category:list", "category:read", "category:write", "category:save"
  65.      * })
  66.      */
  67.     private $id;
  68.     /**
  69.      * @var string|null
  70.      *
  71.      * @Groups({
  72.      *     "product:list", "product:read",
  73.      *     "category:list", "category:read"
  74.      * })
  75.      */
  76.     private $externalId;
  77.     /**
  78.      * @var string|null
  79.      *
  80.      * @Groups({
  81.      *     "product:list", "product:read",
  82.      *     "category:list", "category:read", "category:save"
  83.      * })
  84.      */
  85.     private $customerId;
  86.     /**
  87.      * @var Category|null
  88.      * @Groups({
  89.      *     "list", "read",
  90.      *     "category:list", "category:update", "category:save"
  91.      * })
  92.      * @MaxDepth(1)
  93.      */
  94.     private $parent;
  95.     /**
  96.      * @var Category[]|ArrayCollection
  97.      * @Groups({
  98.      *     "list", "read",
  99.      *     "category:list"
  100.      * })
  101.      */
  102.     private $children;
  103.     /**
  104.      * @var string|null
  105.      *
  106.      * @Groups({
  107.      *     "list",
  108.      *     "product:list", "product:read",
  109.      *     "category:list", "category:read", "category:update", "category:save"
  110.      * })
  111.      */
  112.     private $name;
  113.     /**
  114.      * @var int|null
  115.      *
  116.      * @Groups({
  117.      *     "product:list", "product:read",
  118.      *     "category:list", "category:read", "category:update", "category:save"
  119.      * })
  120.      */
  121.     private $order;
  122.     /**
  123.      * @var Product[]|Collection
  124.      */
  125.     private $products;
  126.     public function __construct()
  127.     {
  128.         $this->children = new ArrayCollection();
  129.         $this->products = new ArrayCollection();
  130.     }
  131.     /**
  132.      * @return string|null
  133.      */
  134.     public function getId(): ?string
  135.     {
  136.         return $this->id;
  137.     }
  138.     /**
  139.      * @param string $id
  140.      *
  141.      * @return self
  142.      */
  143.     public function setId(string $id): self
  144.     {
  145.         $this->id $id;
  146.         return $this;
  147.     }
  148.     /**
  149.      * @return string|null
  150.      */
  151.     public function getCustomerId(): ?string
  152.     {
  153.         return $this->customerId;
  154.     }
  155.     /**
  156.      * @param string $customerId
  157.      *
  158.      * @return self
  159.      */
  160.     public function setCustomerId(string $customerId): self
  161.     {
  162.         $this->customerId $customerId;
  163.         return $this;
  164.     }
  165.     /**
  166.      * @return string|null
  167.      */
  168.     public function getExternalId(): ?string
  169.     {
  170.         return $this->externalId;
  171.     }
  172.     /**
  173.      * @param string|null $externalId
  174.      *
  175.      * @return self
  176.      */
  177.     public function setExternalId(?string $externalId): self
  178.     {
  179.         $this->externalId $externalId;
  180.         return $this;
  181.     }
  182.     /**
  183.      * @return self|null
  184.      */
  185.     public function getParent(): ?Category
  186.     {
  187.         return $this->parent;
  188.     }
  189.     /**
  190.      * @param Category|null $parent
  191.      *
  192.      * @return self
  193.      */
  194.     public function setParent(?Category $parent): self
  195.     {
  196.         $this->parent $parent;
  197.         return $this;
  198.     }
  199.     /**
  200.      * @deprecated Use addChild() instead.
  201.      *     This is stupid, but the ImportableObjectTrait checks for adders using add.ucfirst($type) where $type is the class name.
  202.      *
  203.      * @param Category $category
  204.      *
  205.      * @return $this
  206.      */
  207.     public function addCategory(Category $category): self
  208.     {
  209.         return $this->addChild($category);
  210.     }
  211.     /**
  212.      * @param Category $category
  213.      *
  214.      * @return $this
  215.      */
  216.     public function addChild(Category $category): self
  217.     {
  218.         if (!$this->children->contains($category)) {
  219.             $this->children->add($category);
  220.             $category->setParent($this);
  221.         }
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return Category[]|Collection
  226.      */
  227.     public function getChildren(): Collection
  228.     {
  229.         return $this->children;
  230.     }
  231.     /**
  232.      * @param Category[]|Collection $children
  233.      *
  234.      * @return self
  235.      */
  236.     public function setChildren(Collection $children): self
  237.     {
  238.         $this->children $children;
  239.         return $this;
  240.     }
  241.     /**
  242.      * @return string|null
  243.      */
  244.     public function getName(): ?string
  245.     {
  246.         return $this->name;
  247.     }
  248.     /**
  249.      * @param string $name
  250.      *
  251.      * @return self
  252.      */
  253.     public function setName(string $name): self
  254.     {
  255.         $this->name $name;
  256.         return $this;
  257.     }
  258.     /**
  259.      * @return int|null
  260.      */
  261.     public function getOrder(): ?int
  262.     {
  263.         return $this->order;
  264.     }
  265.     /**
  266.      * @param int|null $order
  267.      *
  268.      * @return self
  269.      */
  270.     public function setOrder(?int $order): self
  271.     {
  272.         $this->order $order;
  273.         return $this;
  274.     }
  275.     /**
  276.      * @return Collection|Product[]
  277.      */
  278.     public function getProduct(): Collection
  279.     {
  280.         return $this->products;
  281.     }
  282.     /**
  283.      * @param Collection|Product[] $products
  284.      *
  285.      * @return $this
  286.      */
  287.     public function setProduct(Collection $products): self
  288.     {
  289.         $this->products $products;
  290.         return $this;
  291.     }
  292.     /**
  293.      * @param Product $product
  294.      *
  295.      * @return $this
  296.      */
  297.     public function addProduct(Product $product): self
  298.     {
  299.         if (!$this->products->contains($product)) {
  300.             $this->products[] = $product;
  301.             $product->addCategory($this);
  302.         }
  303.         return $this;
  304.     }
  305.     /**
  306.      * @param Product $product
  307.      *
  308.      * @return $this
  309.      */
  310.     public function removeProduct(Product $product): self
  311.     {
  312.         if ($this->products->contains($product)) {
  313.             $this->products->removeElement($product);
  314.             $product->removeCategory($this);
  315.         }
  316.         return $this;
  317.     }
  318. }