Release 2.0

This commit is contained in:
Artur
2019-01-25 02:21:35 -02:00
parent 9b78ca484c
commit 8cb8e98154
4 changed files with 234 additions and 166 deletions
+1 -2
View File
@@ -12,6 +12,5 @@
"psr-4" : { "psr-4" : {
"MenuBuilder\\" : "src/MenuBuilder/" "MenuBuilder\\" : "src/MenuBuilder/"
} }
}, }
"require" : { }
} }
+19
View File
@@ -0,0 +1,19 @@
<?php
use MenuBuilder\Menu as Menu;
use MenuBuilder\MenuItem as MenuItem;
include_once './src/MenuBuilder/HtmlBuilder.php';
include_once './src/MenuBuilder/Menu.php';
include_once './src/MenuBuilder/MenuItem.php';
$sidebar = new Menu( array('id' => 'side-menu', 'class' => "nav") );
$sidebar->create_submenu('basico', 'Basico', '', Array(), 2);
$sidebar->add_on_new('basico', '', 'Usuarios');
$sidebar->create_submenu('administrativo', 'Administrativo');
$sidebar->add_on_new('basico', '', 'Geografico', '', Array(), -1);
echo $sidebar->render();
+46 -23
View File
@@ -25,10 +25,10 @@ class Menu {
*/ */
protected $items = []; protected $items = [];
public function items(){ public function items() {
return $this->items; return $this->items;
} }
/** /**
* Create a new Menu instance. * Create a new Menu instance.
* *
@@ -39,12 +39,48 @@ class Menu {
$this->attributes = $attributes; $this->attributes = $attributes;
$this->html = new HtmlBuilder; $this->html = new HtmlBuilder;
} }
public function addDirect($menu){ public function addDirect($menu) {
$this->items[] = $menu; $this->items[] = $menu;
return $this; 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. * Add a menu item.
* *
@@ -94,14 +130,14 @@ class Menu {
return implode(PHP_EOL, $menu); return implode(PHP_EOL, $menu);
} }
public function renderRaw(){ public function renderRaw() {
$menu[] = ""; $menu[] = "";
foreach ($this->items as $item) { foreach ($this->items as $item) {
$menu[] = $item->renderRaw(); $menu[] = $item->renderRaw();
} }
return implode(PHP_EOL, $menu); return implode(PHP_EOL, $menu);
} }
/** /**
* Sort the menu * Sort the menu
* *
@@ -113,19 +149,6 @@ class Menu {
}); });
} }
/*private function merge(){
$new_array = Array();
foreach($items as $key => $item){
foreach($items as $item_){
if( $item->title == $item_->title ){
}
}
}
}*/
/** /**
* Render the menu. * Render the menu.
* *
+168 -141
View File
@@ -3,144 +3,171 @@
namespace MenuBuilder; namespace MenuBuilder;
class MenuItem { class MenuItem {
/**
* HTML builder instance. /**
* * HTML builder instance.
* @var Menu\HtmlBuilder *
*/ * @var Menu\HtmlBuilder
protected $html; */
/** protected $html;
* Menu item title.
* /**
* @var string * Menu item title.
*/ *
protected $title; * @var string
/** */
* Menu item link. protected $title;
*
* @var string /**
*/ * Menu item link.
protected $link; *
/** * @var string
* Nested menu. */
* protected $link;
* @var Menu\Menu
*/ /**
protected $nested; * Menu item name.
/** *
* Array of attributes. * @var string
* */
* @var array public $name;
*/
protected $attributes = []; /**
/** * Nested menu.
* Create a new menu item instance. *
* * @var Menu\Menu
*/ */
protected $nested;
public $icon = '';
public $weight = 0; /**
* Array of attributes.
public function __construct() *
{ * @var array
$this->html = new HtmlBuilder; */
} protected $attributes = [];
/**
* Set the menu item link. /**
* * Create a new menu item instance.
* @param string $link *
* @return Menu\MenuItem */
*/ public $icon = '';
public function link($link) public $weight = 0;
{
$this->link = $link; public function __construct() {
return $this; $this->html = new HtmlBuilder;
} }
/**
* Set the menu item title. /**
* * Set the menu item link.
* @param string $link *
* @return Menu\MenuItem * @param string $link
*/ * @return Menu\MenuItem
public function title($title) */
{ public function link($link) {
$this->title = $title; $this->link = $link;
return $this; return $this;
} }
/**
public function weight($weight = 0){ * Set the menu item name.
$this->weight = $weight; *
return $this; * @param string $name
} * @return Menu\MenuItem
*/
public function icon($icon = ''){ public function name($name) {
$this->icon = $icon; $this->name = $name;
return $this; return $this;
} }
/** /**
* Set the menu item attributes. * Set the menu item title.
* *
* @param array $attributes * @param string $link
* @return Menu\MenuItem * @return Menu\MenuItem
*/ */
public function attributes(array $attributes) public function title($title) {
{ $this->title = $title;
$this->attributes = $attributes; return $this;
return $this; }
}
public function weight($weight = 0) {
/** $this->weight = $weight;
* Nest another menu. return $this;
* }
* @param Menu\Menu $menu
* @return Menu\MenuItem public function icon($icon = '') {
*/ $this->icon = $icon;
public function nest(Menu $menu) return $this;
{ }
$this->nested = $menu;
return $this; /**
} * Set the menu item attributes.
*
/** * @param array $attributes
* Render the menu item. * @return Menu\MenuItem
* */
* @return string public function attributes(array $attributes) {
*/ $this->attributes = $attributes;
public function render() return $this;
{ }
return '<li>'.$this->html->link($this->link, $this->title, $this->icon, $this->attributes).$this->renderNestedMenu().'</li>';
} /**
* Nest another menu.
/** *
* Render the nested menu. * @param Menu\Menu $menu
* * @return Menu\MenuItem
* @return string */
*/ public function nest(Menu $menu) {
public function renderNestedMenu() $this->nested = $menu;
{ return $this;
if ($this->nested) }
{
return $this->nested->render(); /**
} * Nest another menu.
} *
/** * @param Menu\Menu $menu
* Render the menu item. * @return Menu\MenuItem
* */
* @return string public function nested() {
*/ return $this->nested;
public function __toString() }
{
return $this->render(); /**
} * Render the menu item.
*
public static function newMenu($link = '', $title = '', $icon = '' ){ * @return string
$menu = new MenuItem(); */
public function render() {
$menu->link($link); return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '</li>';
$menu->title($title); }
$menu->icon = $icon;
/**
* Render the nested menu.
return $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;
}
}