diff --git a/includes/menu/Group.php b/includes/menu/Group.php index 0a5eb10..8429179 100644 --- a/includes/menu/Group.php +++ b/includes/menu/Group.php @@ -27,7 +27,7 @@ use DomainException; */ class Group { /** - * @var MenuEntry[] + * @var IMenuEntry[] */ private $entries = []; @@ -46,14 +46,14 @@ class Group { * @return array */ public function getEntries() { - $entryPresenter = function ( MenuEntry $entry ) { + $entryPresenter = function ( IMenuEntry $entry ) { $result = [ 'name' => $entry->getName(), 'components' => $entry->getComponents(), ]; - - if ( $entry->isJSOnly() ) { - $result['class'] = 'jsonly'; + $classes = $entry->getCSSClasses(); + if ( $classes ) { + $result[ 'class' ] = implode( ' ', $classes ); } return $result; @@ -74,6 +74,16 @@ class Group { } } + /** + * Insert new menu entry + * @param IMenuEntry $entry + * @throws DomainException When the entry already exists + */ + public function insertEntry( IMenuEntry $entry ) { + $this->throwIfNotUnique( $entry->getName() ); + $this->entries[] = $entry; + } + /** * Insert an entry into the menu. * @@ -119,7 +129,6 @@ class Group { */ public function insertAfter( $targetName, $name, $isJSOnly = false ) { $this->throwIfNotUnique( $name ); - $index = $this->search( $targetName ); if ( $index === -1 ) { diff --git a/includes/menu/IMenuEntry.php b/includes/menu/IMenuEntry.php new file mode 100644 index 0000000..439b7c9 --- /dev/null +++ b/includes/menu/IMenuEntry.php @@ -0,0 +1,48 @@ + text to show + * - href -> href attribute + * - class -> css class applied to it + * @return array + */ + public function getComponents(): array; + +} diff --git a/includes/menu/MenuEntry.php b/includes/menu/MenuEntry.php index 403c2a1..c384bc7 100644 --- a/includes/menu/MenuEntry.php +++ b/includes/menu/MenuEntry.php @@ -20,7 +20,7 @@ namespace MediaWiki\Minerva\Menu; /** * Model for a menu entry. */ -class MenuEntry { +class MenuEntry implements IMenuEntry { private $name; private $isJSOnly; private $components; @@ -43,19 +43,22 @@ class MenuEntry { } /** - * Gets whether the entry should only be shown if JavaScript is disabled - * in the client. + * Return the CSS classes applied to the Menu element * - * @return bool + * @return array */ - public function isJSOnly() { - return $this->isJSOnly; + public function getCSSClasses(): array { + $classes = []; + if ( $this->isJSOnly ) { + $classes[] = 'jsonly'; + } + return $classes; } /** * @return array */ - public function getComponents() { + public function getComponents(): array { return $this->components; } diff --git a/tests/phpunit/menu/MenuEntryTest.php b/tests/phpunit/menu/MenuEntryTest.php index 418e4a5..5563175 100644 --- a/tests/phpunit/menu/MenuEntryTest.php +++ b/tests/phpunit/menu/MenuEntryTest.php @@ -13,7 +13,7 @@ class MenuEntryTest extends \MediaWikiTestCase { /** * @covers ::__construct * @covers ::getName() - * @covers ::isJSOnly() + * @covers ::getCSSClasses() * @covers ::getComponents() */ public function testMenuEntryConstruction() { @@ -21,7 +21,7 @@ class MenuEntryTest extends \MediaWikiTestCase { $isJSOnly = true; $entry = new MenuEntry( $name, $isJSOnly ); $this->assertSame( $name, $entry->getName() ); - $this->assertSame( $isJSOnly, $entry->isJSOnly() ); + $this->assertArrayEquals( [ 'jsonly' ], $entry->getCSSClasses() ); $this->assertSame( [], $entry->getComponents() ); } }