src/Entity/Menu.php line 16

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MenuRepository;
  4. use App\Trait\ActivableTrait;
  5. use App\Trait\DateTrait;
  6. use App\Trait\DeletableTrait;
  7. use DateTimeImmutable;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. #[ORM\Entity(repositoryClassMenuRepository::class)]
  13. class Menu
  14. {
  15.     use ActivableTrait;
  16.     use DateTrait;
  17.     use DeletableTrait;
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column]
  21.     private ?int $id null;
  22.     #[ORM\Column(length255)]
  23.     private ?string $name null;
  24.     #[ORM\Column(length255nullabletrue)]
  25.     private ?string $icon null;
  26.     #[ORM\Column(length255nullabletrue)]
  27.     private ?string $path null;
  28.     #[ORM\Column(typeTypes::JSON)]
  29.     private array $permissions = [];
  30.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'submenus')]
  31.     private ?self $parent null;
  32.     #[ORM\OneToMany(mappedBy'parent'targetEntityself::class, cascade: ['all'])]
  33.     private Collection $submenus;
  34.     #[ORM\ManyToOne]
  35.     #[ORM\JoinColumn(nullabletrue)]
  36.     private ?ModelMenu $modelMenu null;
  37.     #[ORM\ManyToOne(inversedBy'menus')]
  38.     #[ORM\JoinColumn(nullablefalse)]
  39.     private ?Establishment $establishment null;
  40.     public function __construct()
  41.     {
  42.         $this->isActive true;
  43.         if (is_null($this->createdAt)) $this->createdAt = new DateTimeImmutable();
  44.         $this->updatedAt = new DateTimeImmutable();
  45.         $this->submenus = new ArrayCollection();
  46.     }
  47.     public function initialize(ModelMenu $modelMenuEstablishment $establishment): self
  48.     {
  49.         $this->setName($modelMenu->getName());
  50.         $this->setIcon($modelMenu->getIcon());
  51.         $this->setPath($modelMenu->getPath());
  52.         $this->setPermissions($modelMenu->getPermissions());
  53.         $this->setParent($modelMenu->getParent());
  54.         $this->setModelMenu($modelMenu);
  55.         $this->setEstablishment($establishment);
  56.         foreach ($modelMenu->getModelSubmenus() as $modelSubmenu) {
  57.             $subMenu $this->getSubmenus()->filter(fn(Menu $s) => $s->getModelMenu() === $modelSubmenu)->first();
  58.             if (!$subMenu)
  59.                 $subMenu = new Menu();
  60.             $subMenu->setName($modelSubmenu->getName());
  61.             $subMenu->setIcon($modelSubmenu->getIcon());
  62.             $subMenu->setPath($modelSubmenu->getPath());
  63.             $subMenu->setPermissions($modelSubmenu->getPermissions());
  64.             $subMenu->setModelMenu($modelSubmenu);
  65.             $subMenu->setEstablishment($establishment);
  66.             if (is_null($subMenu->getId()))
  67.                 $this->addSubmenu($subMenu);
  68.         }
  69.         return $this;
  70.     }
  71.     public function getId(): ?int
  72.     {
  73.         return $this->id;
  74.     }
  75.     public function getName(): ?string
  76.     {
  77.         return $this->name;
  78.     }
  79.     public function setName(string $name): self
  80.     {
  81.         $this->name $name;
  82.         return $this;
  83.     }
  84.     public function getIcon(): ?string
  85.     {
  86.         return $this->icon;
  87.     }
  88.     public function setIcon(?string $icon): self
  89.     {
  90.         $this->icon $icon;
  91.         return $this;
  92.     }
  93.     public function getPath(): ?string
  94.     {
  95.         return $this->path;
  96.     }
  97.     public function setPath(?string $path): self
  98.     {
  99.         $this->path $path;
  100.         return $this;
  101.     }
  102.     public function getPermissions(): array
  103.     {
  104.         return $this->permissions;
  105.     }
  106.     public function setPermissions(array $permissions): self
  107.     {
  108.         $this->permissions $permissions;
  109.         return $this;
  110.     }
  111.     public function getParent(): ?self
  112.     {
  113.         return $this->parent;
  114.     }
  115.     public function setParent(?self $parent): self
  116.     {
  117.         $this->parent $parent;
  118.         return $this;
  119.     }
  120.     /**
  121.      * @return Collection<int, self>
  122.      */
  123.     public function getSubmenus(): Collection
  124.     {
  125.         return $this->submenus;
  126.     }
  127.     public function addSubmenu(self $submenu): self
  128.     {
  129.         if (!$this->submenus->contains($submenu)) {
  130.             $this->submenus->add($submenu);
  131.             $submenu->setParent($this);
  132.         }
  133.         return $this;
  134.     }
  135.     public function removeSubmenu(self $submenu): self
  136.     {
  137.         if ($this->submenus->removeElement($submenu)) {
  138.             // set the owning side to null (unless already changed)
  139.             if ($submenu->getParent() === $this) {
  140.                 $submenu->setParent(null);
  141.             }
  142.         }
  143.         return $this;
  144.     }
  145.     public function getModelMenu(): ?ModelMenu
  146.     {
  147.         return $this->modelMenu;
  148.     }
  149.     public function setModelMenu(?ModelMenu $modelMenu): self
  150.     {
  151.         $this->modelMenu $modelMenu;
  152.         return $this;
  153.     }
  154.     public function getEstablishment(): ?Establishment
  155.     {
  156.         return $this->establishment;
  157.     }
  158.     public function setEstablishment(?Establishment $establishment): self
  159.     {
  160.         $this->establishment $establishment;
  161.         return $this;
  162.     }
  163.     public function __toString(): string
  164.     {
  165.         return $this->name;
  166.     }
  167. }