From 08b4e4df80beca080a4c9ffc23c81eb30e647121 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Mon, 13 May 2019 19:49:27 +0200 Subject: [PATCH] Menu::getAllMenuParts() method > returns all menu parts (menu, containers with links and links) --- CHANGELOG.md | 1 + src/Menu.php | 25 +++++++++++++++ tests/MenuTest.php | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3ea313..925b42c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Build navigation easily, without any efforts. Library that provides tools to bui 1. Minor refactoring 2. Menu, Link & LinkContainer > add getters (to get content) +3. Menu::getAllMenuParts() method > returns all menu parts (menu, containers with links and links) # 0.0.4 diff --git a/src/Menu.php b/src/Menu.php index 1fa2002..1ecc5bb 100644 --- a/src/Menu.php +++ b/src/Menu.php @@ -11,6 +11,7 @@ namespace Meritoo\Menu; use Meritoo\Common\Collection\Templates; +use Meritoo\Common\Utilities\Arrays; /** * Menu. Has containers with links. @@ -54,6 +55,30 @@ public function getLinksContainers(): array return $this->linksContainers; } + /** + * Returns all menu parts (menu, containers with links and links) + * + * @return MenuPart[] + */ + public function getAllMenuParts(): array + { + $links = []; + + foreach ($this->linksContainers as $container) { + $links[] = $container->getLink(); + } + + $result = array_merge( + [ + $this, + ], + $this->linksContainers, + $links + ); + + return Arrays::getNonEmptyValues($result); + } + /** * Creates new menu * diff --git a/tests/MenuTest.php b/tests/MenuTest.php index a9ee05c..25232bd 100644 --- a/tests/MenuTest.php +++ b/tests/MenuTest.php @@ -56,6 +56,18 @@ public function testGetLinksContainers(string $description, Menu $menu, array $e static::assertEquals($expected, $menu->getLinksContainers(), $description); } + /** + * @param string $description Description of test + * @param Menu $menu Menu to verify + * @param array $expected Expected menu parts + * + * @dataProvider provideMenuToGetAllMenuParts + */ + public function testGetAllMenuParts(string $description, Menu $menu, array $expected): void + { + static::assertEquals($expected, $menu->getAllMenuParts(), $description); + } + public function testRenderWithoutLinksContainers(): void { $menu = new Menu([]); @@ -597,4 +609,71 @@ public function provideMenuToGetLinksContainers(): ?Generator ], ]; } + + public function provideMenuToGetAllMenuParts(): ?Generator + { + yield[ + 'No menu parts', + new Menu([]), + [ + new Menu([]), + ], + ]; + + yield[ + '1 container only', + new Menu([ + new LinkContainer(new Link('', '')), + ]), + [ + new Menu([ + new LinkContainer(new Link('', '')), + ]), + new LinkContainer(new Link('', '')), + new Link('', ''), + ], + ]; + + yield[ + '2 containers', + new Menu([ + new LinkContainer(new Link('Test 1', '')), + new LinkContainer(new Link('Test 2', '/')), + ]), + [ + new Menu([ + new LinkContainer(new Link('Test 1', '')), + new LinkContainer(new Link('Test 2', '/')), + ]), + new LinkContainer(new Link('Test 1', '')), + new LinkContainer(new Link('Test 2', '/')), + new Link('Test 1', ''), + new Link('Test 2', '/'), + ], + ]; + + yield[ + '2 containers - created by static method create()', + Menu::create([ + [ + 'Test 1', + '', + ], + [ + 'Test 2', + '/', + ], + ]), + [ + new Menu([ + new LinkContainer(new Link('Test 1', '')), + new LinkContainer(new Link('Test 2', '/')), + ]), + new LinkContainer(new Link('Test 1', '')), + new LinkContainer(new Link('Test 2', '/')), + new Link('Test 1', ''), + new Link('Test 2', '/'), + ], + ]; + } }