src/Entity/Category.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiFilter;
  4. use ApiPlatform\Metadata\ApiProperty;
  5. use ApiPlatform\Metadata\ApiResource;
  6. use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
  7. use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
  8. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  9. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  10. use DateTimeInterface;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use ApiPlatform\Metadata\Put;
  18. use ApiPlatform\Metadata\Post;
  19. use ApiPlatform\Metadata\Get;
  20. use ApiPlatform\Metadata\GetCollection;
  21. use ApiPlatform\Metadata\Patch;
  22. use ApiPlatform\Metadata\Delete;
  23. use App\Dto\CategoryOutput;
  24. use App\State\CategoryRepresentationProvider;
  25. use App\State\CategoriesRepresentationProvider;
  26. use Symfony\Component\Serializer\Annotation\MaxDepth;
  27. #[ApiResource(
  28.     normalizationContext: ['groups' => ['category:read'], 'enable_max_depth' => 'true'],
  29.     denormalizationContext: ['groups' => ['category:write'], 'enable_max_depth' => 'true'],
  30.     paginationItemsPerPage500,
  31.     operations: [
  32.         new Get(),
  33.         new GetCollection(),
  34.         new Put(security"is_granted('ROLE_USER')",),
  35.         new Post(security"is_granted('ROLE_USER')",),
  36.         new Patch(security"is_granted('ROLE_USER')",),
  37.         new Delete(security"is_granted('ROLE_USER')",),
  38.         new GetCollection(
  39.             uriTemplate"/categoriesRepresentation",
  40.             outputCategoryOutput::class,
  41.             providerCategoriesRepresentationProvider::class,
  42.         ),
  43.         new Get(
  44.             uriTemplate"/categoryRepresentation/{slug}",
  45.             outputCategoryOutput::class,
  46.             providerCategoryRepresentationProvider::class,
  47.         ),    
  48.     ]
  49. )]
  50. #[Gedmo\Tree(type"nested")]
  51. #[ApiFilter(OrderFilter::class)]
  52. #[ApiFilter(DateFilter::class, properties: ["createdAt"])]
  53. #[ApiFilter(SearchFilter::class)]
  54. #[ApiFilter(SearchFilter::class, properties: ["name" => "partial"])]
  55. #[ApiFilter(BooleanFilter::class, properties: ["public" => "exact"])]
  56. #[UniqueEntity('slug')]
  57. #[ORM\Entity(repositoryClass\Gedmo\Tree\Entity\Repository\NestedTreeRepository::class)]
  58. class Category implements DateCmsInterfaceAuthorCmsInterface
  59. {
  60.     #[ApiProperty(identifierfalse)]
  61.     #[ORM\Id]
  62.     #[ORM\GeneratedValue]
  63.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::INTEGER)]
  64.     #[Groups(['category:read'])]
  65.     private ?int $id null;
  66.     #[ApiProperty(types: ["https://schema.org/name"])]
  67.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  68.     #[Groups(['category:read''category:write'])]
  69.     private ?string $name null;
  70.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength1024nullabletrue)]
  71.     #[Groups(['category:read''category:write'])]
  72.     private ?string $short null;
  73.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::BOOLEAN)]
  74.     #[Groups(['category:read''category:write'])]
  75.     private ?bool $public true;
  76.     #[Gedmo\Slug(fields: ["name"])]
  77.     #[ApiProperty(identifiertrue)]
  78.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGnullabletruelength80uniquetrue)]
  79.     #[Groups(['category:read'])]
  80.     private ?string $slug null;
  81.     #[ORM\ManyToOne(targetEntityUser::class)]
  82.     #[Groups(['category:read''category:write'])]
  83.     private ?\App\Entity\User $author null;
  84.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::DATETIME_MUTABLE)]
  85.     private ?\DateTimeInterface $createdAt null;
  86.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::DATETIME_MUTABLE)]
  87.     private ?\DateTimeInterface $updatedAt null;
  88.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::INTEGER)]
  89.     #[Groups(['category:read''category:write'])]
  90.     private ?int $type null;
  91.     /**
  92.      * @var \Doctrine\Common\Collections\Collection<\App\Entity\Content>
  93.      */
  94.     #[ORM\ManyToMany(targetEntityContent::class, mappedBy'category')]
  95.     #[Groups(['category:read'])]
  96.     private \Doctrine\Common\Collections\Collection $contents;
  97.     #[Gedmo\TreeLeft]
  98.     #[ORM\Column(name'lft'type\Doctrine\DBAL\Types\Types::INTEGER)]
  99.     #[Groups(['category:read''category:write'])]
  100.     private ?int $lft null;
  101.     #[Gedmo\TreeLevel]
  102.     #[ORM\Column(name'lvl'type\Doctrine\DBAL\Types\Types::INTEGER)]
  103.     #[Groups(['category:read''category:write'])]
  104.     private ?int $lvl null;
  105.     #[Gedmo\TreeRight]
  106.     #[ORM\Column(name'rgt'type\Doctrine\DBAL\Types\Types::INTEGER)]
  107.     #[Groups(['category:read''category:write'])]
  108.     private ?int $rgt null;
  109.     #[Gedmo\TreeRoot]
  110.     #[ORM\ManyToOne(targetEntity'Category')]
  111.     #[ORM\JoinColumn(name'tree_root'onDelete'CASCADE')]
  112.     #[Groups(['category:read''category:write'])]
  113.     private ?\App\Entity\Category $root null;
  114.     ##[MaxDepth(1)]
  115.     #[ApiFilter(SearchFilter::class)]
  116.     #[Gedmo\TreeParent]
  117.     #[ORM\ManyToOne(targetEntity'Category'inversedBy'children')]
  118.     #[ORM\JoinColumn(name'parent_id'onDelete'CASCADE')]
  119.     #[Groups(['category:read''category:write'])]
  120.     #[ApiProperty(readableLinkfalsewritableLinkfalse)]
  121.     private ?\App\Entity\Category $parent null;
  122.     /**
  123.      * @var string|null
  124.      */
  125.     #[Groups(['category:read'])]
  126.     public $parentName;
  127.     /**
  128.      * @var \Doctrine\Common\Collections\Collection<\App\Entity\Category>
  129.      */
  130.     ##[MaxDepth(1)]
  131.     #[ORM\OneToMany(targetEntity'Category'mappedBy'parent')]
  132.     #[ORM\OrderBy(['lft' => 'ASC'])]
  133.     #[Groups(['category:read''category:write'])]
  134.     private \Doctrine\Common\Collections\Collection $children;
  135.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::INTEGERnullabletrue)]
  136.     #[Groups(['category:read''category:write'])]
  137.     private ?int $position null;
  138.     /**
  139.      * @var MediaObject|null
  140.      *
  141.      */
  142.     #[ApiProperty(types: ["https://schema.org/image"])]
  143.     #[ORM\ManyToOne(targetEntityMediaObject::class)]
  144.     #[ORM\JoinColumn]
  145.     #[Groups(['category:read''category:write'])]
  146.     private ?\App\Entity\MediaObject $image null;
  147.     #[ApiFilter(BooleanFilter::class)]
  148.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::BOOLEAN)]
  149.     #[Groups(['category:read''category:write'])]
  150.     private ?bool $showInFooter false;
  151.     /**
  152.      * @var \Doctrine\Common\Collections\Collection<\App\Entity\Product>
  153.      */
  154.     #[ORM\ManyToMany(targetEntityProduct::class, mappedBy'category')]
  155.     private \Doctrine\Common\Collections\Collection $products;
  156.     public function __construct()
  157.     {
  158.         $this->children = new ArrayCollection();
  159.         $this->contents = new ArrayCollection();
  160.         $this->products = new ArrayCollection();
  161.     }
  162.     public function getId(): ?int
  163.     {
  164.         return $this->id;
  165.     }
  166.     public function getName(): ?string
  167.     {
  168.         return $this->name;
  169.     }
  170.     public function setName(string $name): self
  171.     {
  172.         $this->name $name;
  173.         return $this;
  174.     }
  175.     public function getShort(): ?string
  176.     {
  177.         return $this->short;
  178.     }
  179.     public function setShort(?string $short): self
  180.     {
  181.         $this->short $short;
  182.         return $this;
  183.     }
  184.     public function getPublic(): ?bool
  185.     {
  186.         return $this->public;
  187.     }
  188.     public function setPublic(?bool $public): self
  189.     {
  190.         $this->public $public;
  191.         return $this;
  192.     }
  193.     public function getSlug(): ?string
  194.     {
  195.         return $this->slug;
  196.     }
  197.     public function setSlug(string $slug): self
  198.     {
  199.         $this->slug $slug;
  200.         return $this;
  201.     }
  202.     public function getAuthor(): ?User
  203.     {
  204.         return $this->author;
  205.     }
  206.     public function setAuthor(?User $author): self
  207.     {
  208.         $this->author $author;
  209.         return $this;
  210.     }
  211.     public function getCreatedAt(): ?\DateTimeInterface
  212.     {
  213.         return $this->createdAt;
  214.     }
  215.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  216.     {
  217.         $this->createdAt $createdAt;
  218.         return $this;
  219.     }
  220.     public function getUpdatedAt(): ?\DateTimeInterface
  221.     {
  222.         return $this->updatedAt;
  223.     }
  224.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  225.     {
  226.         $this->updatedAt $updatedAt;
  227.         return $this;
  228.     }
  229.     public function getType(): ?int
  230.     {
  231.         return $this->type;
  232.     }
  233.     public function setType(int $type): self
  234.     {
  235.         $this->type $type;
  236.         return $this;
  237.     }
  238.     public function getParent(): ?self
  239.     {
  240.         return $this->parent;
  241.     }
  242.     public function setParent(?self $parent): self
  243.     {
  244.         $this->parent $parent;
  245.         return $this;
  246.     }
  247.     public function getPosition(): ?int
  248.     {
  249.         return $this->position;
  250.     }
  251.     public function setPosition(?int $position): self
  252.     {
  253.         $this->position $position;
  254.         return $this;
  255.     }
  256.     public function getImage(): ?MediaObject
  257.     {
  258.         return $this->image;
  259.     }
  260.     public function setImage(?MediaObject $image): self
  261.     {
  262.         $this->image $image;
  263.         return $this;
  264.     }
  265.     public function getLft(): ?int
  266.     {
  267.         return $this->lft;
  268.     }
  269.     public function setLft(int $lft): self
  270.     {
  271.         $this->lft $lft;
  272.         return $this;
  273.     }
  274.     public function getLvl(): ?int
  275.     {
  276.         return $this->lvl;
  277.     }
  278.     public function setLvl(int $lvl): self
  279.     {
  280.         $this->lvl $lvl;
  281.         return $this;
  282.     }
  283.     public function getRgt(): ?int
  284.     {
  285.         return $this->rgt;
  286.     }
  287.     public function setRgt(int $rgt): self
  288.     {
  289.         $this->rgt $rgt;
  290.         return $this;
  291.     }
  292.     public function getRoot(): ?self
  293.     {
  294.         return $this->root;
  295.     }
  296.     public function setRoot(?self $root): self
  297.     {
  298.         $this->root $root;
  299.         return $this;
  300.     }
  301.     /**
  302.      * @return Collection|Category[]
  303.      */
  304.     public function getChildren(): Collection
  305.     {
  306.         return $this->children;
  307.     }
  308.     public function addChild(Category $child): self
  309.     {
  310.         if (!$this->children->contains($child)) {
  311.             $this->children[] = $child;
  312.             $child->setParent($this);
  313.         }
  314.         return $this;
  315.     }
  316.     public function removeChild(Category $child): self
  317.     {
  318.         if ($this->children->removeElement($child)) {
  319.             // set the owning side to null (unless already changed)
  320.             if ($child->getParent() === $this) {
  321.                 $child->setParent(null);
  322.             }
  323.         }
  324.         return $this;
  325.     }
  326.     public function getShowInFooter(): ?bool
  327.     {
  328.         return $this->showInFooter;
  329.     }
  330.     public function setShowInFooter(?bool $showInFooter): self
  331.     {
  332.         $this->showInFooter $showInFooter;
  333.         return $this;
  334.     }
  335.     /**
  336.      * @return Collection|Content[]
  337.      */
  338.     public function getContents(): Collection
  339.     {
  340.         return $this->contents;
  341.     }
  342.     public function addContent(Content $content): self
  343.     {
  344.         if (!$this->contents->contains($content)) {
  345.             $this->contents[] = $content;
  346.             $content->addCategory($this);
  347.         }
  348.         return $this;
  349.     }
  350.     public function removeContent(Content $content): self
  351.     {
  352.         if ($this->contents->removeElement($content)) {
  353.             $content->removeCategory($this);
  354.         }
  355.         return $this;
  356.     }
  357.     /**
  358.      * @return Collection|Product[]
  359.      */
  360.     public function getProducts(): Collection
  361.     {
  362.         return $this->products;
  363.     }
  364.     public function addProduct(Product $product): self
  365.     {
  366.         if (!$this->products->contains($product)) {
  367.             $this->products[] = $product;
  368.             $product->addCategory($this);
  369.         }
  370.         return $this;
  371.     }
  372.     public function removeProduct(Product $product): self
  373.     {
  374.         if ($this->products->removeElement($product)) {
  375.             $product->removeCategory($this);
  376.         }
  377.         return $this;
  378.     }
  379. }