From 6905b85d67a2c41c8a95ad73854147993292ae58 Mon Sep 17 00:00:00 2001 From: Piotr Miazga Date: Tue, 14 May 2019 12:31:54 +0200 Subject: [PATCH] Provide IMenuEntry interface The Group shouldn't depend upon concrete MenuEntry definition. Different Menus can present different MenuElements. Code should allow easy extensions, not limit only to single MenuEntry definition. Changes: - introduced IMenuEntry interface - MenuEntry implements IMenuEntry - removed isJSOnly from logic as it's related only to one menu element (Watchstar) and Group shouldn't be aware of some special handling for some elements. The IMenuEntry shouldn't define this method - getName, getComponents, getCSSClasses should have defined return types Bug: 1221792 Change-Id: I0646df734e869c26bfa8c3a772200e8258a8acce --- includes/menu/Group.php | 21 ++++++++---- includes/menu/IMenuEntry.php | 48 ++++++++++++++++++++++++++++ includes/menu/MenuEntry.php | 17 ++++++---- tests/phpunit/menu/MenuEntryTest.php | 4 +-- 4 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 includes/menu/IMenuEntry.php 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() ); } }