From 8cb8e98154adc11c2a22ef3f751d32811ae5401c Mon Sep 17 00:00:00 2001 From: Artur Date: Fri, 25 Jan 2019 02:21:35 -0200 Subject: [PATCH] Release 2.0 --- composer.json | 3 +- index.php | 19 +++ src/MenuBuilder/Menu.php | 69 +++++--- src/MenuBuilder/MenuItem.php | 309 +++++++++++++++++++---------------- 4 files changed, 234 insertions(+), 166 deletions(-) create mode 100644 index.php diff --git a/composer.json b/composer.json index d68e880..9032281 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,5 @@ "psr-4" : { "MenuBuilder\\" : "src/MenuBuilder/" } - }, - "require" : { } + } } \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..af25bd2 --- /dev/null +++ b/index.php @@ -0,0 +1,19 @@ + '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(); \ No newline at end of file diff --git a/src/MenuBuilder/Menu.php b/src/MenuBuilder/Menu.php index 6a4ad27..1f343a7 100755 --- a/src/MenuBuilder/Menu.php +++ b/src/MenuBuilder/Menu.php @@ -25,10 +25,10 @@ class Menu { */ protected $items = []; - public function items(){ - return $this->items; + public function items() { + return $this->items; } - + /** * Create a new Menu instance. * @@ -39,12 +39,48 @@ class Menu { $this->attributes = $attributes; $this->html = new HtmlBuilder; } - - public function addDirect($menu){ - $this->items[] = $menu; - return $this; + + 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. * @@ -94,14 +130,14 @@ class Menu { return implode(PHP_EOL, $menu); } - public function renderRaw(){ + public function renderRaw() { $menu[] = ""; foreach ($this->items as $item) { $menu[] = $item->renderRaw(); } return implode(PHP_EOL, $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. * diff --git a/src/MenuBuilder/MenuItem.php b/src/MenuBuilder/MenuItem.php index f1ae71c..4897926 100644 --- a/src/MenuBuilder/MenuItem.php +++ b/src/MenuBuilder/MenuItem.php @@ -3,144 +3,171 @@ namespace MenuBuilder; class MenuItem { - /** - * HTML builder instance. - * - * @var Menu\HtmlBuilder - */ - protected $html; - /** - * Menu item title. - * - * @var string - */ - protected $title; - /** - * Menu item link. - * - * @var string - */ - protected $link; - /** - * 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 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; - } - - /** - * Render the menu item. - * - * @return string - */ - public function render() - { - return '
  • '.$this->html->link($this->link, $this->title, $this->icon, $this->attributes).$this->renderNestedMenu().'
  • '; - } - - /** - * 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; - } -} \ No newline at end of file + + /** + * 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() { + return '
  • ' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '
  • '; + } + + /** + * 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; + } + +}