<?php
namespace App\Entity;
use App\Repository\MenuRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: MenuRepository::class)]
#[UniqueEntity('key')]
class Menu
{
use TimestampableEntity; //Hook timestampable behavior. Updates createdAt, updatedAt fields
use BlameableEntity; //Hook blameable behavior. Updates createdBy, updatedBy fields
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column()]
private ?int $id = null;
#[ORM\Column(length: 100, unique: true)]
#[Assert\NotNull()]
#[Assert\Length(max: 100)]
private ?string $key = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $label = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $route = null;
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
private $roles = [];
#[ORM\Column()]
#[Assert\NotNull()]
private ?int $menuOrder = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $icon = null;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
private ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private Collection $children;
#[ORM\Column(options: ['default' => true])]
private ?bool $active = true;
public function __construct()
{
$this->children = new ArrayCollection();
}
public function __toString()
{
return $this->getKey();
}
public function getId(): ?int
{
return $this->id;
}
public function getKey(): ?string
{
return $this->key;
}
public function setKey(string $key): self
{
$this->key = $key;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getRoute(): ?string
{
return $this->route;
}
public function setRoute(?string $route): self
{
$this->route = $route;
return $this;
}
public function getRoles(): ?array
{
return $this->roles;
}
public function setRoles(?array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getMenuOrder(): ?int
{
return $this->menuOrder;
}
public function setMenuOrder(int $menuOrder): self
{
$this->menuOrder = $menuOrder;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildren(): Collection
{
return $this->children;
}
/**
* @return Collection<int, self>
*/
public function getActiveChildren(): Collection
{
return $this->children->filter(function (self $child) {
return $child->isActive();
});
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
}