<?php
namespace App\CmsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(
* name="menu_item",
* indexes={
* @ORM\Index(name="menu_menu_index", columns={"menu_id"}),
* @ORM\Index(name="menu_parent_index", columns={"parent_id"}),
* @ORM\Index(name="menu_item_level_index", columns={"level"}),
* @ORM\Index(name="menu_item_published_index", columns={"published"})
* }
* )
* @ORM\Entity(repositoryClass="App\CmsBundle\Repository\MenuItemRepository")
* @ORM\HasLifecycleCallbacks()
*/
class MenuItem
{
use IdTrait, TitleTrait, TimeStampedTrait, PositionTrait, PublishedTrait;
/**
* @var string
*
* @ORM\Column(
* name="title",
* type="string",
* length=255,
* nullable=false
* )
*/
private $title;
/**
* @var string
*
* @ORM\Column(
* name="subtitle",
* type="string",
* length=255,
* nullable=true
* )
*/
private $subtitle;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
/**
* @ORM\Column(type="integer")
*/
private $level;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Menu", inversedBy="menuItems")
* @ORM\JoinColumn(nullable=false)
*/
private $menu;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $target;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $additional;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $icon;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\MenuItem", inversedBy="childs" )
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\MenuItem", mappedBy="parent", orphanRemoval=true, fetch="EAGER")
*/
private $childs;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default" : false})
*/
private $isMostPopular;
public function __construct()
{
$this->childs = new ArrayCollection();
$this->level = 0;
$this->hidden = false;
}
public function __toString(): string
{
return $this->title;
}
public function getSubtitle(): ?string
{
return $this->subtitle;
}
public function setSubtitle(?string $subtitle): self
{
$this->subtitle = $subtitle;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getMenu(): ?Menu
{
return $this->menu;
}
public function setMenu(?Menu $menu): self
{
$this->menu = $menu;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): self
{
$this->path = $path;
return $this;
}
public function getAdditional(): ?string
{
return $this->additional;
}
public function setAdditional(?string $additional): self
{
$this->additional = $additional;
return $this;
}
public function getTarget(): ?int
{
return $this->target;
}
public function setTarget(?int $target): self
{
$this->target = $target;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
$this->level++;
if ($parent) {
while ($parent = $parent->getParent()) {
$this->level++;
}
}
return $this;
}
/**
* @return Collection|self[]
*/
public function getChilds(): Collection
{
return $this->childs;
}
public function addChild(self $child): self
{
if (!$this->childs->contains($child)) {
$this->childs[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->childs->contains($child)) {
$this->childs->removeElement($child);
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function relationTitle(): string
{
$title = $this->getTitle();
$parent = $this;
while ($parent = $parent->getParent()) {
$title = $parent->getTitle() . ' > ' . $title;
}
return $title;
}
public function getIsMostPopular(): ?bool
{
return $this->isMostPopular;
}
public function setIsMostPopular(?bool $isMostPopular): self
{
$this->isMostPopular = $isMostPopular;
return $this;
}
}