.
*/
namespace Fisharebest\Webtrees;
/**
* System for generating menus.
*/
class Menu {
/** @var string The text to be displayed in the mneu */
private $label;
/** @var string The target URL or href*/
private $link;
/** @var string The CSS class used to style this menu item */
private $class;
/** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */
private $attrs;
/** @var Menu[] An optional list of sub-menus. */
private $submenus;
/**
* Constructor for the menu class
*
* @param string $label The label for the menu item
* @param string $link The target URL
* @param string $class A CSS class
* @param string[] $attrs Optional attributes, such as onclick or data-xxx
* @param Menu[] $submenus Any submenus
*/
public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = []) {
$this
->setLabel($label)
->setLink($link)
->setClass($class)
->setAttrs($attrs)
->setSubmenus($submenus);
}
/**
* Render this menu using Bootstrap4 markup
*
* @return string
*/
public function bootstrap4() {
if ($this->submenus) {
$submenus = '';
foreach ($this->submenus as $submenu) {
$attrs = '';
foreach ($submenu->attrs as $key => $value) {
$attrs .= ' ' . $key . '="' . e($value) . '"';
}
$class = trim('dropdown-item ' . $submenu->class);
$submenus .= '' . $submenu->label . '';
}
$class = trim('nav-item dropdown ' . $this->class);
return
'
' .
'' .
$this->label .
'' .
'' .
'';
} else {
$attrs = '';
foreach ($this->attrs as $key => $value) {
$attrs .= ' ' . $key . '="' . e($value) . '"';
}
$class = trim('nav-item ' . $this->class);
return '' . $this->label . '';
}
}
/**
* Get the optional attributes.
*
* @return string[]
*/
public function getAttrs() {
return $this->attrs;
}
/**
* Set the optional attributes.
*
* @param string[] $attrs
*
* @return $this
*/
public function setAttrs(array $attrs) {
$this->attrs = $attrs;
return $this;
}
/**
* Get the class.
*
* @return string
*/
public function getClass() {
return $this->class;
}
/**
* Set the class.
*
* @param string $class
*
* @return $this
*/
public function setClass($class) {
$this->class = $class;
return $this;
}
/**
* Get the label.
*
* @return string
*/
public function getLabel() {
return $this->label;
}
/**
* Set the label.
*
* @param string $label
*
* @return $this
*/
public function setLabel($label) {
$this->label = $label;
return $this;
}
/**
* Get the link.
*
* @return string
*/
public function getLink() {
return $this->link;
}
/**
* Set the link.
*
* @param string $link
*
* @return $this
*/
public function setLink($link) {
$this->link = $link;
return $this;
}
/**
* Add a submenu to this menu
*
* @param Menu $menu
*
* @return $this
*/
public function addSubmenu($menu) {
$this->submenus[] = $menu;
return $this;
}
/**
* Render this menu as an HTML list
*
* @return string
*/
public function getMenuAsList() {
$attrs = '';
foreach ($this->attrs as $key => $value) {
$attrs .= ' ' . $key . '="' . e($value) . '"';
}
if ($this->link) {
$link = ' href="' . $this->link . '"';
} else {
$link = '';
}
$html = '' . $this->label . '';
if ($this->submenus) {
$html .= '';
foreach ($this->submenus as $submenu) {
$html .= $submenu->getMenuAsList();
}
$html .= '
';
}
return '' . $html . '';
}
/**
* Get the sub-menus.
*
* @return Menu[]
*/
public function getSubmenus() {
return $this->submenus;
}
/**
* Set the sub-menus.
*
* @param Menu[] $submenus
*
* @return $this
*/
public function setSubmenus(array $submenus) {
$this->submenus = $submenus;
return $this;
}
}