diff --git a/class-Menu.php b/class-Menu.php index 293907a..f767209 100644 --- a/class-Menu.php +++ b/class-Menu.php @@ -1,116 +1,109 @@ . /** - * Handle a menu tree. + * Handle a menu tree */ class Menu { - private $menuEntries = []; + private $entries = []; private $tree = []; private $rootUid; /** * Specify the default root identifier. * * @param string $rootUid The default menu entry root identifier. */ - function __construct($rootUid = 'root') { + function __construct( $rootUid = 'root' ) { $this->rootUid = $rootUid; } /** * Get the singleton instance * * @return self */ public function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Append a single or an array of menu entries. * - * @param array|MenuEntry $menuEntries + * @param array|MenuEntry $entries */ - public function add($menuEntries) { - force_array($menuEntries); - - foreach($menuEntries as $menuEntry) { - $this->menuEntries[ $menuEntry->uid ] = $menuEntry; - + public function add( $entries ) { + force_array( $entries ); + foreach( $entries as $menuEntry ) { + $this->entries[ $menuEntry->uid ] = $menuEntry; force_array( $menuEntry->parentUid ); - - foreach($menuEntry->parentUid as $parentUid) { + foreach( $menuEntry->parentUid as $parentUid ) { if( $parentUid === null ) { $parentUid = $this->rootUid; } - $this->setParent($menuEntry->uid, $parentUid); + $this->setParent( $menuEntry->uid, $parentUid ); } } } /** * Force a parent->children relation. * * @param string $uid Children menu entry identifier. * @param string $parentUid Parent menu entry identifier. */ public function setParent($uid, $parentUid) { if( ! isset( $this->tree[ $parentUid ] ) || ! is_array( $this->tree[ $parentUid ] ) ) { $this->tree[ $parentUid ] = []; } $this->tree[ $parentUid ][] = $uid; } /** - * Get a single menu entry. + * Get a single menu entry if registered * - * @param string $uid Menu entry identifier. + * @param string $uid Menu entry identifier * @return MenuEntry */ - public function getMenuEntry($uid) { - if( isset( $this->menuEntries[ $uid ] ) ) { - return $this->menuEntries[ $uid ]; + public function getMenuEntry( $uid ) { + if( isset( $this->entries[ $uid ] ) ) { + return $this->entries[ $uid ]; } - error( "the menu entry $uid was not found" ); } /** * Get an array of menu entries. * * @param string|null $parentUid Parent menu entry identifier of the parent. The default menu entry root identifier as default. * @return array */ - public function getChildrenMenuEntries($parentUid = null) { - if($parentUid === null) { + public function getChildrenMenuEntries( $parentUid = null ) { + if( $parentUid === null ) { $parentUid = $this->rootUid; } - - $menuEntries = []; - + $entries = []; if( isset( $this->tree[ $parentUid ] ) ) { - foreach($this->tree[ $parentUid ] as $uid) { - $menuEntries[] = $this->menuEntries[ $uid ]; + foreach( $this->tree[ $parentUid ] as $uid ) { + $entries[] = $this->entries[ $uid ]; } } - - return $menuEntries; + return $entries; } }