<?php
namespace App\Entity\Website\Menu;
use App\Entity\BaseEntity;
use App\Repository\Website\Menu\MenuItemRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
#[ORM\Entity(repositoryClass: MenuItemRepository::class)]
class MenuItem extends BaseEntity
{
#[ORM\Id]
#[ORM\Column(type: 'guid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?string $id;
#[ORM\Column(length: 255)]
private ?string $label = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $url = null;
#[ORM\Column(nullable: true)]
private ?int $menuItemOrder = null;
#[ORM\ManyToOne(inversedBy: 'menuItems')]
#[ORM\JoinColumn(nullable: false)]
private ?Menu $menu = null;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'menuItems')]
private ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private Collection $menuItems;
#[ORM\Column(length: 255)]
private ?string $type = null;
#[ORM\Column(length: 255)]
private ?string $target = null;
public function __construct()
{
parent::__construct();
$this->menuItems = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): static
{
$this->label = $label;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): static
{
$this->url = $url;
return $this;
}
public function getMenuItemOrder(): ?int
{
return $this->menuItemOrder;
}
public function setMenuItemOrder(?int $menuItemOrder): static
{
$this->menuItemOrder = $menuItemOrder;
return $this;
}
public function getMenu(): ?Menu
{
return $this->menu;
}
public function setMenu(?Menu $menu): static
{
$this->menu = $menu;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): static
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getMenuItems(): Collection
{
return $this->menuItems;
}
public function addMenuItem(self $menuItem): static
{
if (!$this->menuItems->contains($menuItem)) {
$this->menuItems->add($menuItem);
$menuItem->setParent($this);
}
return $this;
}
public function removeMenuItem(self $menuItem): static
{
if ($this->menuItems->removeElement($menuItem)) {
// set the owning side to null (unless already changed)
if ($menuItem->getParent() === $this) {
$menuItem->setParent(null);
}
}
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): static
{
$this->type = $type;
return $this;
}
public function getTarget(): ?string
{
return $this->target;
}
public function setTarget( $target): static
{
switch (true) {
case $target instanceof \App\Entity\Shop\Product\Product:
$this->target = $target->getId();
break;
case $target instanceof \App\Entity\Shop\Product\ProductCategory:
$this->target = $target->getId();
break;
case $target instanceof \App\Entity\Shop\Page\Page:
$this->target = $target->getId();
break;
default:
$this->target = $target;
// حالت پیشفرض
}
return $this;
}
}