commit 9e0d343f367e096836b43c516901950dd5e6338f Author: ahwelp Date: Tue Aug 14 19:35:43 2018 -0300 First Commit diff --git a/.idea/MenuBuilder.iml b/.idea/MenuBuilder.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/MenuBuilder.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..0eefe32 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..29bd15b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..43764e7 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d68e880 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name" : "ahwelp/menubuilder", + "description" : "Different Menus", + "type" : "library", + "authors" : [ + { + "name" : "ahwelp", + "email" : "ahwelp@ahwelp.com" + } + ], + "autoload" : { + "psr-4" : { + "MenuBuilder\\" : "src/MenuBuilder/" + } + }, + "require" : { } +} \ No newline at end of file diff --git a/src/MenuBuilder/HtmlBuilder.php b/src/MenuBuilder/HtmlBuilder.php new file mode 100644 index 0000000..9cdb063 --- /dev/null +++ b/src/MenuBuilder/HtmlBuilder.php @@ -0,0 +1,43 @@ +attributes($attributes).'>'.$icon.$this->entities($title).''; + } + /** + * Convert an HTML string to entities. + * + * @param string $value + * @return string + */ + public function entities($value) + { + return htmlentities($value, ENT_QUOTES, 'UTF-8', false); + } + /** + * Build an HTML attribute string from an array. + * + * @param array $attributes + * @return string + */ + public function attributes(array $attributes) + { + $html = []; + foreach ($attributes as $key => $value) + { + if (is_null($value)) continue; + if (is_numeric($key)) $key = $value; + $html[] = $key.'="'.$this->entities($value).'"'; + } + return empty($html) ? '' : ' '.implode(' ', $html); + } +} \ No newline at end of file diff --git a/src/MenuBuilder/Menu.php b/src/MenuBuilder/Menu.php new file mode 100755 index 0000000..6a4ad27 --- /dev/null +++ b/src/MenuBuilder/Menu.php @@ -0,0 +1,138 @@ +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[] = ""; + + foreach ($this->items as $item) { + $menu[] = $item->render(); + } + + $menu[] = ''; + + 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(); + } + +} diff --git a/src/MenuBuilder/MenuItem.php b/src/MenuBuilder/MenuItem.php new file mode 100644 index 0000000..f1ae71c --- /dev/null +++ b/src/MenuBuilder/MenuItem.php @@ -0,0 +1,146 @@ +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