|
|
@@ -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 '<li>'.$this->html->link($this->link, $this->title, $this->icon, $this->attributes).$this->renderNestedMenu().'</li>';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 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;
|
|
|
- }
|
|
|
-}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '</li>';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|