Change name
This commit is contained in:
Executable
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Menus;
|
||||
|
||||
class HtmlBuilder {
|
||||
/**
|
||||
* Create an HTML link.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $title
|
||||
* @param array $attributes
|
||||
* @return string
|
||||
*/
|
||||
public function link($url, $title, $icon = "", $attributes = [])
|
||||
{
|
||||
return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$icon.$this->entities($title).'</a>';
|
||||
}
|
||||
/**
|
||||
* Convert an HTML string to entities.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function entities($value)
|
||||
{
|
||||
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
|
||||
}
|
||||
/**
|
||||
* Build an HTML attribute string from an array.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return string
|
||||
*/
|
||||
public function attributes(array $attributes)
|
||||
{
|
||||
$html = [];
|
||||
foreach ($attributes as $key => $value)
|
||||
{
|
||||
if (is_null($value)) continue;
|
||||
if (is_numeric($key)) $key = $value;
|
||||
$html[] = $key.'="'.$this->entities($value).'"';
|
||||
}
|
||||
return empty($html) ? '' : ' '.implode(' ', $html);
|
||||
}
|
||||
}
|
||||
Executable
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Menus;
|
||||
|
||||
class Menu {
|
||||
|
||||
/**
|
||||
* HTML builder instance.
|
||||
*
|
||||
* @var \JasonLewis\Menu\HtmlBuilder
|
||||
*/
|
||||
protected $html;
|
||||
|
||||
/**
|
||||
* Array of attribtues.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* Array of menu items.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $items = [];
|
||||
|
||||
public function items() {
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Menu instance.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($attributes = []) {
|
||||
$this->attributes = $attributes;
|
||||
$this->html = new HtmlBuilder;
|
||||
}
|
||||
|
||||
public function addDirect($menu) {
|
||||
$this->items[] = $menu;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create_submenu($name, $title, $icon = '', $attributes = [], $weight = 0) {
|
||||
$menu_item = false;
|
||||
foreach ($this->items as $item) {
|
||||
if ($item->name == $name) {
|
||||
$menu_item = $item;
|
||||
//$menu_item = $item->nested();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$menu_item) {
|
||||
$menu = new MenuItem();
|
||||
$menu = $menu->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
|
||||
$menu = $menu->nest(new Menu(Array('class' => 'nav nav-ident')));
|
||||
$menu_item = $menu->nested();
|
||||
$this->items[] = $menu;
|
||||
} else {
|
||||
if (!$title == null) {
|
||||
$menu_item->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
|
||||
$menu_item = $menu_item->nested();
|
||||
}else{
|
||||
$menu_item = $menu_item->nested();
|
||||
}
|
||||
}
|
||||
return $menu_item;
|
||||
}
|
||||
|
||||
public function add_on($name, MenuItem $item) {
|
||||
$this->create_submenu($name)->items[] = $item;
|
||||
}
|
||||
|
||||
public function add_on_new($name, $link, $title, $icon = '', $attributes = [], $weight = 0) {
|
||||
$menu_item = $this->create_submenu($name, null);
|
||||
$menu = new MenuItem();
|
||||
$menu_item->items[] = $menu->link($link)->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a menu item.
|
||||
*
|
||||
* @param string $link
|
||||
* @param string $title
|
||||
* @param array $attributes
|
||||
* @return \JasonLewis\Menu\Menu
|
||||
*/
|
||||
public function add($link, $title, $icon = '', $attributes = [], $weight = 0) {
|
||||
$menu = new MenuItem();
|
||||
$this->items[] = $menu->link($link)->title($title)->icon($icon)->attributes($attributes)->weight($weight);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nest a menu on the previous item.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return \JasonLewis\Menu\Menu
|
||||
*/
|
||||
public function nestMenu($attributes = []) {
|
||||
$key = count($this->items) - 1;
|
||||
|
||||
$this->items[$key]->nest($menu = new Menu($attributes));
|
||||
|
||||
return $menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the menu.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render() {
|
||||
$this->sort();
|
||||
$attributes = $this->html->attributes($this->attributes);
|
||||
|
||||
$menu[] = "<ul{$attributes}>";
|
||||
|
||||
foreach ($this->items as $item) {
|
||||
$menu[] = $item->render();
|
||||
}
|
||||
|
||||
$menu[] = '</ul>';
|
||||
|
||||
return implode(PHP_EOL, $menu);
|
||||
}
|
||||
|
||||
public function renderRaw() {
|
||||
$menu[] = "";
|
||||
foreach ($this->items as $item) {
|
||||
$menu[] = $item->renderRaw();
|
||||
}
|
||||
return implode(PHP_EOL, $menu);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the menu
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function sort() {
|
||||
usort($this->items, function($a, $b) { // anonymous function
|
||||
return $a->weight - $b->weight;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the menu.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+180
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Menus;
|
||||
|
||||
class MenuItem {
|
||||
|
||||
/**
|
||||
* HTML builder instance.
|
||||
*
|
||||
* @var Menu\HtmlBuilder
|
||||
*/
|
||||
protected $html;
|
||||
|
||||
/**
|
||||
* Menu item title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* Menu item link.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $link;
|
||||
|
||||
/**
|
||||
* Menu item name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* Nested menu.
|
||||
*
|
||||
* @var Menu\Menu
|
||||
*/
|
||||
protected $nested;
|
||||
|
||||
/**
|
||||
* Array of attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* Create a new menu item instance.
|
||||
*
|
||||
*/
|
||||
public $icon = '';
|
||||
public $weight = 0;
|
||||
|
||||
public function __construct() {
|
||||
$this->html = new HtmlBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the menu item link.
|
||||
*
|
||||
* @param string $link
|
||||
* @return Menu\MenuItem
|
||||
*/
|
||||
public function link($link) {
|
||||
$this->link = $link;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the menu item name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return Menu\MenuItem
|
||||
*/
|
||||
public function name($name) {
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the menu item title.
|
||||
*
|
||||
* @param string $link
|
||||
* @return Menu\MenuItem
|
||||
*/
|
||||
public function title($title) {
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function weight($weight = 0) {
|
||||
$this->weight = $weight;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function icon($icon = '') {
|
||||
$this->icon = $icon;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the menu item attributes.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return Menu\MenuItem
|
||||
*/
|
||||
public function attributes(array $attributes) {
|
||||
$this->attributes = $attributes;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nest another menu.
|
||||
*
|
||||
* @param Menu\Menu $menu
|
||||
* @return Menu\MenuItem
|
||||
*/
|
||||
public function nest(Menu $menu) {
|
||||
$this->nested = $menu;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nest another menu.
|
||||
*
|
||||
* @param Menu\Menu $menu
|
||||
* @return Menu\MenuItem
|
||||
*/
|
||||
public function nested() {
|
||||
return $this->nested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the menu item.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render() {
|
||||
if( empty($this->nested) ){
|
||||
return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . '</li>';
|
||||
}
|
||||
if ( !empty($this->nested->items()) ) {
|
||||
return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '</li>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the nested menu.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function renderNestedMenu() {
|
||||
if ($this->nested) {
|
||||
return $this->nested->render();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the menu item.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
public static function newMenu($link = '', $title = '', $icon = '') {
|
||||
$menu = new MenuItem();
|
||||
|
||||
$menu->link($link);
|
||||
$menu->title($title);
|
||||
$menu->icon = $icon;
|
||||
|
||||
|
||||
return $menu;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user