From 5c2619dcb594138f1a7a3daf12ce3b780d5b0509 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 4 Sep 2015 15:04:29 +0200 Subject: [PATCH] Add class NavigationItemContainer refs #5600 --- .../Application/Modules/MenuItemContainer.php | 120 +----------------- library/Icinga/Application/Modules/Module.php | 2 +- .../Modules/NavigationItemContainer.php | 117 +++++++++++++++++ 3 files changed, 125 insertions(+), 114 deletions(-) create mode 100644 library/Icinga/Application/Modules/NavigationItemContainer.php diff --git a/library/Icinga/Application/Modules/MenuItemContainer.php b/library/Icinga/Application/Modules/MenuItemContainer.php index 6f9ec7f93..2d9f176f4 100644 --- a/library/Icinga/Application/Modules/MenuItemContainer.php +++ b/library/Icinga/Application/Modules/MenuItemContainer.php @@ -4,24 +4,10 @@ namespace Icinga\Application\Modules; /** - * Container for module menus + * Container for module menu items */ -class MenuItemContainer +class MenuItemContainer extends NavigationItemContainer { - /** - * This menu item's name - * - * @var string - */ - protected $name; - - /** - * This menu item's properties - * - * @var array - */ - protected $properties; - /** * This menu item's children * @@ -29,65 +15,6 @@ class MenuItemContainer */ protected $children; - /** - * Create a new MenuItemContainer - * - * @param string $name - * @param array $properties - */ - public function __construct($name, array $properties = null) - { - $this->name = $name; - $this->children = array(); - $this->properties = $properties; - } - - /** - * Set this menu item's name - * - * @param string $name - * - * @return $this - */ - public function setName($name) - { - $this->name = $name; - return $this; - } - - /** - * Return this menu item's name - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Set this menu item's properties - * - * @param array $properties - * - * @return $this - */ - public function setProperties(array $properties) - { - $this->properties = $properties; - return $this; - } - - /** - * Return this menu item's properties - * - * @return array - */ - public function getProperties() - { - return $this->properties ?: array(); - } - /** * Set this menu item's children * @@ -108,54 +35,21 @@ class MenuItemContainer */ public function getChildren() { - return $this->children; + return $this->children ?: array(); } /** - * Add a new child + * Add a new sub menu * * @param string $name * @param array $properties * - * @return MenuItemContainer The newly added menu item + * @return MenuItemContainer The newly added sub menu */ - public function add($name, array $properties = null) + public function add($name, array $properties = array()) { - $child = new static($name, $properties); + $child = new MenuItemContainer($name, $properties); $this->children[] = $child; return $child; } - - /** - * Allow dynamic setters and getters for properties - * - * @param string $name - * @param array $arguments - * - * @return mixed - * - * @throws ProgrammingError In case the called method is not supported - */ - public function __call($name, $arguments) - { - if (method_exists($this, $name)) { - return call_user_method_array($name, $this, $arguments); - } - - $type = substr($name, 0, 3); - if ($type !== 'set' && $type !== 'get') { - throw new ProgrammingError( - 'Dynamic method %s is not supported. Only getters (get*) and setters (set*) are.', - $name - ); - } - - $propertyName = strtolower(join('_', preg_split('~(?=[A-Z])~', lcfirst(substr($name, 3))))); - if ($type === 'set') { - $this->properties[$propertyName] = $arguments[0]; - return $this; - } else { // $type === 'get' - return array_key_exists($propertyName, $this->properties) ? $this->properties[$propertyName] : null; - } - } } diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index 89c36e974..ef5d90a4f 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -339,7 +339,7 @@ class Module * * @return MenuItemContainer */ - protected function menuSection($name, array $properties = null) + protected function menuSection($name, array $properties = array()) { if (array_key_exists($name, $this->menuItems)) { $this->menuItems[$name]->setProperties($properties); diff --git a/library/Icinga/Application/Modules/NavigationItemContainer.php b/library/Icinga/Application/Modules/NavigationItemContainer.php new file mode 100644 index 000000000..b59d52f60 --- /dev/null +++ b/library/Icinga/Application/Modules/NavigationItemContainer.php @@ -0,0 +1,117 @@ +name = $name; + $this->properties = $properties; + } + + /** + * Set this menu item's name + * + * @param string $name + * + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * Return this menu item's name + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Set this menu item's properties + * + * @param array $properties + * + * @return $this + */ + public function setProperties(array $properties) + { + $this->properties = $properties; + return $this; + } + + /** + * Return this menu item's properties + * + * @return array + */ + public function getProperties() + { + return $this->properties ?: array(); + } + + /** + * Allow dynamic setters and getters for properties + * + * @param string $name + * @param array $arguments + * + * @return mixed + * + * @throws ProgrammingError In case the called method is not supported + */ + public function __call($name, $arguments) + { + if (method_exists($this, $name)) { + return call_user_method_array($name, $this, $arguments); + } + + $type = substr($name, 0, 3); + if ($type !== 'set' && $type !== 'get') { + throw new ProgrammingError( + 'Dynamic method %s is not supported. Only getters (get*) and setters (set*) are.', + $name + ); + } + + $propertyName = strtolower(join('_', preg_split('~(?=[A-Z])~', lcfirst(substr($name, 3))))); + if ($type === 'set') { + $this->properties[$propertyName] = $arguments[0]; + return $this; + } else { // $type === 'get' + return array_key_exists($propertyName, $this->properties) ? $this->properties[$propertyName] : null; + } + } +}