src/Shop/Doctrine/Entity/Taxonomy.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Shop\Doctrine\Entity;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Shop\Doctrine\Repository\TaxonomyRepository;
  7. use App\Shop\Filter\TaxonomyFilter;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping\Column;
  11. use Doctrine\ORM\Mapping\Entity;
  12. use Doctrine\ORM\Mapping\GeneratedValue;
  13. use Doctrine\ORM\Mapping\Id;
  14. use Doctrine\ORM\Mapping\ManyToOne;
  15. use Doctrine\ORM\Mapping\OneToMany;
  16. use Doctrine\ORM\Mapping\Table;
  17. use Gedmo\Mapping\Annotation as Gedmo;
  18. use Symfony\Component\Serializer\Annotation\Groups;
  19. use Symfony\Component\Validator\Constraints\NotBlank;
  20. /**
  21.  * @Gedmo\Tree(type="nested")
  22.  */
  23. #[ApiResource(
  24.     collectionOperations: ['get'],
  25.     itemOperations: ['get'],
  26.     attributes: ['pagination_enabled' => false],
  27.     normalizationContext: ['groups' => ['read']]
  28. )]
  29. #[ApiFilter(TaxonomyFilter::class)]
  30. #[Entity(repositoryClassTaxonomyRepository::class)]
  31. #[Table(name'`shop_taxonomy`')]
  32. class Taxonomy
  33. {
  34.     #[Id]
  35.     #[Column(type'integer')]
  36.     #[GeneratedValue]
  37.     #[Groups(['read''product'])]
  38.     private ?int $id null;
  39.     #[Column]
  40.     #[NotBlank]
  41.     #[Groups(['read''product'])]
  42.     private string $name;
  43.     /**
  44.      * @Gedmo\TreeLevel
  45.      */
  46.     #[Column(name'lvl'type'integer')]
  47.     #[Groups('read')]
  48.     private int $level;
  49.     /**
  50.      * @Gedmo\TreeLeft
  51.      */
  52.     #[Column(name'lft'type'integer')]
  53.     private int $left;
  54.     /**
  55.      * @Gedmo\TreeRight
  56.      */
  57.     #[Column(name'rgt'type'integer')]
  58.     private int $right;
  59.     #[Groups('read')]
  60.     private int $numberOfProducts 0;
  61.     /**
  62.      * @Gedmo\TreeParent
  63.      */
  64.     #[ManyToOne(targetEntityTaxonomy::class, inversedBy'children')]
  65.     private ?Taxonomy $parent null;
  66.     /**
  67.      * @var Collection<int, Taxonomy>
  68.      */
  69.     #[OneToMany(mappedBy'parent'targetEntityTaxonomy::class)]
  70.     #[Groups('read')]
  71.     private Collection $children;
  72.     /**
  73.      * @var Collection<int, Product>
  74.      */
  75.     #[OneToMany(mappedBy'taxonomy'targetEntityProduct::class, fetch'EXTRA_LAZY')]
  76.     private Collection $products;
  77.     public function __construct()
  78.     {
  79.         $this->children = new ArrayCollection();
  80.         $this->products = new ArrayCollection();
  81.     }
  82.     public function __toString(): string
  83.     {
  84.         return $this->getName();
  85.     }
  86.     public function getId(): ?int
  87.     {
  88.         return $this->id;
  89.     }
  90.     public function getName(): string
  91.     {
  92.         return $this->name;
  93.     }
  94.     public function setName(string $name): void
  95.     {
  96.         $this->name $name;
  97.     }
  98.     public function getLevel(): int
  99.     {
  100.         return $this->level;
  101.     }
  102.     public function setLevel(int $level): void
  103.     {
  104.         $this->level $level;
  105.     }
  106.     public function getLeft(): int
  107.     {
  108.         return $this->left;
  109.     }
  110.     public function setLeft(int $left): void
  111.     {
  112.         $this->left $left;
  113.     }
  114.     public function getRight(): int
  115.     {
  116.         return $this->right;
  117.     }
  118.     public function setRight(int $right): void
  119.     {
  120.         $this->right $right;
  121.     }
  122.     public function getParent(): ?Taxonomy
  123.     {
  124.         return $this->parent;
  125.     }
  126.     public function setParent(?Taxonomy $parent): void
  127.     {
  128.         $this->parent $parent;
  129.     }
  130.     /**
  131.      * @return Collection<int, Taxonomy>
  132.      */
  133.     public function getChildren(): Collection
  134.     {
  135.         return $this->children;
  136.     }
  137.     /**
  138.      * @return Collection<int, Product>
  139.      */
  140.     public function getProducts(): Collection
  141.     {
  142.         return $this->products;
  143.     }
  144.     /**
  145.      * @return array<int|string, array<string, int|string|null>|int|string>
  146.      */
  147.     public function getTree(): array
  148.     {
  149.         $data = [
  150.             'key' => $this->id,
  151.             'name' => $this->name,
  152.             'rootdistance' => $this->level,
  153.             'children' => $this->children->count(),
  154.         ];
  155.         if (null !== $this->parent) {
  156.             $data['parent'] = $this->parent->id;
  157.         }
  158.         return array_merge(
  159.             [$data],
  160.             ...$this->children->map(static fn (Taxonomy $taxonomy) => $taxonomy->getTree())->toArray()
  161.         );
  162.     }
  163.     public function getNumberOfProducts(): int
  164.     {
  165.         return $this->numberOfProducts;
  166.     }
  167.     public function setNumberOfProducts(int $numberOfProducts): void
  168.     {
  169.         $this->numberOfProducts $numberOfProducts;
  170.     }
  171. }