Menu.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace MenuBuilder;
  3. class Menu {
  4. /**
  5. * HTML builder instance.
  6. *
  7. * @var \JasonLewis\Menu\HtmlBuilder
  8. */
  9. protected $html;
  10. /**
  11. * Array of attribtues.
  12. *
  13. * @var array
  14. */
  15. protected $attributes = [];
  16. /**
  17. * Array of menu items.
  18. *
  19. * @var array
  20. */
  21. protected $items = [];
  22. public function items() {
  23. return $this->items;
  24. }
  25. /**
  26. * Create a new Menu instance.
  27. *
  28. * @param array $attributes
  29. * @return void
  30. */
  31. public function __construct($attributes = []) {
  32. $this->attributes = $attributes;
  33. $this->html = new HtmlBuilder;
  34. }
  35. public function addDirect($menu) {
  36. $this->items[] = $menu;
  37. return $this;
  38. }
  39. public function create_submenu($name, $title, $icon = '', $attributes = [], $weight = 0) {
  40. $menu_item = false;
  41. foreach ($this->items as $item) {
  42. if ($item->name == $name) {
  43. $menu_item = $item;
  44. //$menu_item = $item->nested();
  45. break;
  46. }
  47. }
  48. if (!$menu_item) {
  49. $menu = new MenuItem();
  50. $menu = $menu->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
  51. $menu = $menu->nest(new Menu(Array('class' => 'nav nav-ident')));
  52. $menu_item = $menu->nested();
  53. $this->items[] = $menu;
  54. } else {
  55. if (!$title == null) {
  56. $menu_item->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
  57. $menu_item = $menu_item->nested();
  58. }else{
  59. $menu_item = $menu_item->nested();
  60. }
  61. }
  62. return $menu_item;
  63. }
  64. public function add_on($name, MenuItem $item) {
  65. $this->create_submenu($name)->items[] = $item;
  66. }
  67. public function add_on_new($name, $link, $title, $icon = '', $attributes = [], $weight = 0) {
  68. $menu_item = $this->create_submenu($name, null);
  69. $menu = new MenuItem();
  70. $menu_item->items[] = $menu->link($link)->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
  71. }
  72. /**
  73. * Add a menu item.
  74. *
  75. * @param string $link
  76. * @param string $title
  77. * @param array $attributes
  78. * @return \JasonLewis\Menu\Menu
  79. */
  80. public function add($link, $title, $icon = '', $attributes = [], $weight = 0) {
  81. $menu = new MenuItem();
  82. $this->items[] = $menu->link($link)->title($title)->icon($icon)->attributes($attributes)->weight($weight);
  83. return $this;
  84. }
  85. /**
  86. * Nest a menu on the previous item.
  87. *
  88. * @param array $attributes
  89. * @return \JasonLewis\Menu\Menu
  90. */
  91. public function nestMenu($attributes = []) {
  92. $key = count($this->items) - 1;
  93. $this->items[$key]->nest($menu = new Menu($attributes));
  94. return $menu;
  95. }
  96. /**
  97. * Render the menu.
  98. *
  99. * @return string
  100. */
  101. public function render() {
  102. $this->sort();
  103. $attributes = $this->html->attributes($this->attributes);
  104. $menu[] = "<ul{$attributes}>";
  105. foreach ($this->items as $item) {
  106. $menu[] = $item->render();
  107. }
  108. $menu[] = '</ul>';
  109. return implode(PHP_EOL, $menu);
  110. }
  111. public function renderRaw() {
  112. $menu[] = "";
  113. foreach ($this->items as $item) {
  114. $menu[] = $item->renderRaw();
  115. }
  116. return implode(PHP_EOL, $menu);
  117. }
  118. /**
  119. * Sort the menu
  120. *
  121. * @return void
  122. */
  123. private function sort() {
  124. usort($this->items, function($a, $b) { // anonymous function
  125. return $a->weight - $b->weight;
  126. });
  127. }
  128. /**
  129. * Render the menu.
  130. *
  131. * @return string
  132. */
  133. public function __toString() {
  134. return $this->render();
  135. }
  136. }