| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?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();
- }
- }
|