src/Entity/Menu.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MenuRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Blameable\Traits\BlameableEntity;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. #[ORM\Entity(repositoryClassMenuRepository::class)]
  12. #[UniqueEntity('key')]
  13. class Menu
  14. {
  15.     use TimestampableEntity//Hook timestampable behavior. Updates createdAt, updatedAt fields 
  16.     use BlameableEntity//Hook blameable behavior. Updates createdBy, updatedBy fields
  17.     #[ORM\Id]
  18.     #[ORM\GeneratedValue(strategy"IDENTITY")]
  19.     #[ORM\Column()]
  20.     private ?int $id null;
  21.     #[ORM\Column(length100uniquetrue)]
  22.     #[Assert\NotNull()]
  23.     #[Assert\Length(max100)]
  24.     private ?string $key null;
  25.     #[ORM\Column(length255nullabletrue)]
  26.     #[Assert\Length(max255)]
  27.     private ?string $label null;
  28.     #[ORM\Column(length255nullabletrue)]
  29.     #[Assert\Length(max255)]
  30.     private ?string $route null;
  31.     #[ORM\Column(type'json'nullabletrueoptions: ['jsonb' => true])]
  32.     private $roles = [];
  33.     #[ORM\Column()]
  34.     #[Assert\NotNull()]
  35.     private ?int $menuOrder null;
  36.     #[ORM\Column(length255nullabletrue)]
  37.     #[Assert\Length(max255)]
  38.     private ?string $icon null;
  39.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'children')]
  40.     private ?self $parent null;
  41.     #[ORM\OneToMany(mappedBy'parent'targetEntityself::class)]
  42.     private Collection $children;
  43.     #[ORM\Column(options: ['default' => true])]
  44.     private ?bool $active true;
  45.     public function __construct()
  46.     {
  47.         $this->children = new ArrayCollection();
  48.     }
  49.     public function __toString()
  50.     {
  51.         return $this->getKey();
  52.     }
  53.     public function getId(): ?int
  54.     {
  55.         return $this->id;
  56.     }
  57.     public function getKey(): ?string
  58.     {
  59.         return $this->key;
  60.     }
  61.     public function setKey(string $key): self
  62.     {
  63.         $this->key $key;
  64.         return $this;
  65.     }
  66.     public function getLabel(): ?string
  67.     {
  68.         return $this->label;
  69.     }
  70.     public function setLabel(string $label): self
  71.     {
  72.         $this->label $label;
  73.         return $this;
  74.     }
  75.     public function getRoute(): ?string
  76.     {
  77.         return $this->route;
  78.     }
  79.     public function setRoute(?string $route): self
  80.     {
  81.         $this->route $route;
  82.         return $this;
  83.     }
  84.     public function getRoles(): ?array
  85.     {
  86.         return $this->roles;
  87.     }
  88.     public function setRoles(?array $roles): self
  89.     {
  90.         $this->roles $roles;
  91.         return $this;
  92.     }
  93.     public function getMenuOrder(): ?int
  94.     {
  95.         return $this->menuOrder;
  96.     }
  97.     public function setMenuOrder(int $menuOrder): self
  98.     {
  99.         $this->menuOrder $menuOrder;
  100.         return $this;
  101.     }
  102.     public function getIcon(): ?string
  103.     {
  104.         return $this->icon;
  105.     }
  106.     public function setIcon(?string $icon): self
  107.     {
  108.         $this->icon $icon;
  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 getChildren(): Collection
  124.     {
  125.         return $this->children;
  126.     }
  127.     /**
  128.      * @return Collection<int, self>
  129.      */
  130.     public function getActiveChildren(): Collection
  131.     {
  132.         return $this->children->filter(function (self $child) {
  133.             return $child->isActive();
  134.         });
  135.     }
  136.     
  137.     public function addChild(self $child): self
  138.     {
  139.         if (!$this->children->contains($child)) {
  140.             $this->children->add($child);
  141.             $child->setParent($this);
  142.         }
  143.         return $this;
  144.     }
  145.     public function removeChild(self $child): self
  146.     {
  147.         if ($this->children->removeElement($child)) {
  148.             // set the owning side to null (unless already changed)
  149.             if ($child->getParent() === $this) {
  150.                 $child->setParent(null);
  151.             }
  152.         }
  153.         return $this;
  154.     }
  155.     public function isActive(): ?bool
  156.     {
  157.         return $this->active;
  158.     }
  159.     public function setActive(bool $active): self
  160.     {
  161.         $this->active $active;
  162.         return $this;
  163.     }
  164. }