src/Entity/Category.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Controller\Action\ListDomainsWithStatusReportController;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use ApiPlatform\Metadata\ApiResource;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  10. use ApiPlatform\Metadata\ApiFilter;
  11. use ApiPlatform\Metadata\Delete;
  12. use ApiPlatform\Metadata\Get;
  13. use ApiPlatform\Metadata\GetCollection;
  14. use ApiPlatform\Metadata\Post;
  15. use ApiPlatform\Metadata\Put;
  16. use App\Controller\Action\CategoryTreeAction;
  17. use App\Controller\Action\CategoryTreeFilterAction;
  18. #[ORM\Table(name'category')]
  19. #[ORM\Index(name'parent_id'columns: ['parent_id'])]
  20. #[ORM\Entity]
  21. #[ApiResource(
  22.     operations: [
  23.         new GetCollection(),
  24.         new Get(
  25.             name'_api_/categories/tree',
  26.             uriTemplate'/categories/tree',
  27.             controllerCategoryTreeAction::class,
  28.             writefalse,
  29.             readfalse
  30.         ),  new Get(
  31.             name'_api_/categories/tree/filter',
  32.             uriTemplate'/categories/tree/filter',
  33.             controllerCategoryTreeFilterAction::class,
  34.             writefalse,
  35.             readfalse
  36.         ),
  37.         new Post(),
  38.         new Get(),
  39.         new Put(),
  40.         new Delete(),
  41.         new Post(
  42.             uriTemplate'/domains_with_status',
  43.             controllerListDomainsWithStatusReportController::class,
  44.             readfalse,
  45.             writefalse,
  46.             name'_api_/domains_with_status'
  47.         ),
  48.     ],
  49.     normalizationContext: ['groups' => ['category.get''document.get']]
  50. )
  51. ]
  52. #[ApiFilter(
  53.     SearchFilter::class,
  54.     properties: [
  55.         'type' => 'exact'
  56.     ]
  57. )]
  58. class Category
  59. {
  60.     //@maybe this should be converted to the new php8 enum supported feature
  61.     const THEME_TYPE 'THEME';
  62.     const DOMAIN_TYPE 'DOMAIN';
  63.     const SUB_DOMAIN_TYPE 'SUB_DOMAIN';
  64.     #[ORM\Column(name'id'type'integer'nullablefalse)]
  65.     #[ORM\Id]
  66.     #[ORM\GeneratedValue(strategy'IDENTITY')]
  67.     #[Groups(['category.get''document.get''plan.get'])]
  68.     private int $id;
  69.     #[ORM\Column(name'type'type'string'length10nullabletrue)]
  70.     #[Groups('category.get''document.get''plan.get')]
  71.     private ?string $type null;
  72.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'categories')]
  73.     #[Groups([ 'document.get'])]
  74.     private ?self $parent null;
  75.     #[ORM\OneToMany(mappedBy'parent'targetEntityself::class)]
  76.     #[Groups('category.get')]
  77.     private Collection $categories;
  78.     #[ORM\Column(length255)]
  79.     #[Groups(['category.get''document.get''plan.get'])]
  80.     private ?string $name null;
  81.     public function __construct()
  82.     {
  83.         $this->categories = new ArrayCollection();
  84.     }
  85.     public function getId(): ?int
  86.     {
  87.         return $this->id;
  88.     }
  89.     public function getType(): ?string
  90.     {
  91.         return $this->type;
  92.     }
  93.     public function setType(?string $type): self
  94.     {
  95.         $this->type $type;
  96.         return $this;
  97.     }
  98.     public function getParent(): ?self
  99.     {
  100.         return $this->parent;
  101.     }
  102.     public function setParent(?self $parent): self
  103.     {
  104.         $this->parent $parent;
  105.         return $this;
  106.     }
  107.     /**
  108.      * @return Collection<int, self>
  109.      */
  110.     public function getCategories(): Collection
  111.     {
  112.         return $this->categories;
  113.     }
  114.     public function addCategory(self $category): self
  115.     {
  116.         if (!$this->categories->contains($category)) {
  117.             $this->categories->add($category);
  118.             $category->setParent($this);
  119.         }
  120.         return $this;
  121.     }
  122.     public function removeCategory(self $category): self
  123.     {
  124.         if ($this->categories->removeElement($category)) {
  125.             // set the owning side to null (unless already changed)
  126.             if ($category->getParent() === $this) {
  127.                 $category->setParent(null);
  128.             }
  129.         }
  130.         return $this;
  131.     }
  132.     public function getName(): ?string
  133.     {
  134.         return $this->name;
  135.     }
  136.     public function setName(string $name): self
  137.     {
  138.         $this->name $name;
  139.         return $this;
  140.     }
  141. }