|
@@ -0,0 +1,138 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace MenuBuilder;
|
|
|
|
|
+
|
|
|
|
|
+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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 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;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /*private function merge(){
|
|
|
|
|
+ $new_array = Array();
|
|
|
|
|
+ foreach($items as $key => $item){
|
|
|
|
|
+ foreach($items as $item_){
|
|
|
|
|
+ if( $item->title == $item_->title ){
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }*/
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Render the menu.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ public function __toString() {
|
|
|
|
|
+ return $this->render();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|