Skip to content

Commit cac6d90

Browse files
committed
Move dropdown accessors to new menu class
1 parent 54338f0 commit cac6d90

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

packages/framework/src/Framework/Features/Navigation/MainNavigationMenu.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7-
use Hyde\Facades\Config;
8-
use BadMethodCallException;
9-
107
/** @deprecated Use the new NavigationMenu class instead */
118
class MainNavigationMenu extends BaseNavigationMenu
129
{
@@ -15,26 +12,4 @@ public static function create(): static
1512
{
1613
return new static(GeneratesMainNavigationMenu::handle()->getItems());
1714
}
18-
19-
public function hasDropdowns(): bool
20-
{
21-
return $this->dropdownsEnabled() && count($this->getDropdowns()) >= 1;
22-
}
23-
24-
/** @return array<string, DropdownNavItem> */
25-
public function getDropdowns(): array
26-
{
27-
if (! $this->dropdownsEnabled()) {
28-
throw new BadMethodCallException('Dropdowns are not enabled. Enable it by setting `hyde.navigation.subdirectories` to `dropdown`.');
29-
}
30-
31-
return $this->items->filter(function (NavItem $item): bool {
32-
return $item instanceof DropdownNavItem;
33-
})->values()->all();
34-
}
35-
36-
protected function dropdownsEnabled(): bool
37-
{
38-
return Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown';
39-
}
4015
}

packages/framework/src/Framework/Features/Navigation/NavigationMenu.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7+
use Hyde\Facades\Config;
8+
use BadMethodCallException;
79
use Illuminate\Support\Collection;
810
use Illuminate\Contracts\Support\Arrayable;
911

@@ -22,4 +24,26 @@ public function getItems(): Collection
2224
{
2325
return $this->items;
2426
}
27+
28+
public function hasDropdowns(): bool
29+
{
30+
return $this->dropdownsEnabled() && count($this->getDropdowns()) >= 1;
31+
}
32+
33+
/** @return array<string, DropdownNavItem> */
34+
public function getDropdowns(): array
35+
{
36+
if (! $this->dropdownsEnabled()) {
37+
throw new BadMethodCallException('Dropdowns are not enabled. Enable it by setting `hyde.navigation.subdirectories` to `dropdown`.');
38+
}
39+
40+
return $this->items->filter(function (NavItem $item): bool {
41+
return $item instanceof DropdownNavItem;
42+
})->values()->all();
43+
}
44+
45+
protected function dropdownsEnabled(): bool
46+
{
47+
return Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown';
48+
}
2549
}

packages/framework/tests/Feature/NavigationMenuTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,28 +243,28 @@ public function testPagesInSubdirectoriesAreNotAddedToTheNavigationMenuWithConfi
243243
public function testHasDropdownsReturnsFalseWhenThereAreNoDropdowns()
244244
{
245245
config(['hyde.navigation.subdirectories' => 'dropdown']);
246-
$menu = $this->createMainNavigationMenu();
246+
$menu = $this->createNewMainNavigationMenu();
247247
$this->assertFalse($menu->hasDropdowns());
248248
}
249249

250250
public function testHasDropdownsReturnsTrueWhenThereAreDropdowns()
251251
{
252252
config(['hyde.navigation.subdirectories' => 'dropdown']);
253253
Routes::addRoute((new MarkdownPage('foo/bar'))->getRoute());
254-
$menu = $this->createMainNavigationMenu();
254+
$menu = $this->createNewMainNavigationMenu();
255255
$this->assertTrue($menu->hasDropdowns());
256256
}
257257

258258
public function testHasDropdownsAlwaysReturnsFalseWhenDropdownsAreDisabled()
259259
{
260260
Routes::addRoute((new MarkdownPage('foo/bar'))->getRoute());
261-
$this->assertFalse($this->createMainNavigationMenu()->hasDropdowns());
261+
$this->assertFalse($this->createNewMainNavigationMenu()->hasDropdowns());
262262
}
263263

264264
public function testGetDropdownsReturnsEmptyArrayThereAreNoDropdowns()
265265
{
266266
config(['hyde.navigation.subdirectories' => 'dropdown']);
267-
$menu = $this->createMainNavigationMenu();
267+
$menu = $this->createNewMainNavigationMenu();
268268
$this->assertCount(0, $menu->getDropdowns());
269269
$this->assertSame([], $menu->getDropdowns());
270270
}
@@ -273,7 +273,7 @@ public function testGetDropdownsReturnsCorrectArrayWhenThereAreDropdowns()
273273
{
274274
config(['hyde.navigation.subdirectories' => 'dropdown']);
275275
Routes::addRoute((new MarkdownPage('foo/bar'))->getRoute());
276-
$menu = $this->createMainNavigationMenu();
276+
$menu = $this->createNewMainNavigationMenu();
277277
$this->assertCount(1, $menu->getDropdowns());
278278

279279
$this->assertEquals([
@@ -288,7 +288,7 @@ public function testGetDropdownsWithMultipleItems()
288288

289289
Routes::addRoute((new MarkdownPage('foo/bar'))->getRoute());
290290
Routes::addRoute((new MarkdownPage('foo/baz'))->getRoute());
291-
$menu = $this->createMainNavigationMenu();
291+
$menu = $this->createNewMainNavigationMenu();
292292

293293
$this->assertCount(1, $menu->getDropdowns());
294294

@@ -308,7 +308,7 @@ public function testGetDropdownsWithMultipleDropdowns()
308308
Routes::addRoute((new MarkdownPage('foo/baz'))->getRoute());
309309
Routes::addRoute((new MarkdownPage('cat/hat'))->getRoute());
310310

311-
$menu = $this->createMainNavigationMenu();
311+
$menu = $this->createNewMainNavigationMenu();
312312

313313
$this->assertCount(2, $menu->getDropdowns());
314314

@@ -328,7 +328,7 @@ public function testGetDropdownsThrowsExceptionWhenDisabled()
328328
$this->expectException(BadMethodCallException::class);
329329
$this->expectExceptionMessage('Dropdowns are not enabled. Enable it by setting `hyde.navigation.subdirectories` to `dropdown`.');
330330

331-
$menu = $this->createMainNavigationMenu();
331+
$menu = $this->createNewMainNavigationMenu();
332332
$menu->getDropdowns();
333333
}
334334

@@ -338,7 +338,7 @@ public function testDocumentationPagesDoNotGetAddedToDropdowns()
338338

339339
Routes::addRoute((new DocumentationPage('foo'))->getRoute());
340340
Routes::addRoute((new DocumentationPage('bar/baz'))->getRoute());
341-
$menu = $this->createMainNavigationMenu();
341+
$menu = $this->createNewMainNavigationMenu();
342342

343343
$this->assertFalse($menu->hasDropdowns());
344344
$this->assertCount(0, $menu->getDropdowns());
@@ -351,7 +351,7 @@ public function testBlogPostsDoNotGetAddedToDropdowns()
351351
Routes::addRoute((new MarkdownPost('foo'))->getRoute());
352352
Routes::addRoute((new MarkdownPost('bar/baz'))->getRoute());
353353

354-
$menu = $this->createMainNavigationMenu();
354+
$menu = $this->createNewMainNavigationMenu();
355355
$this->assertFalse($menu->hasDropdowns());
356356
$this->assertCount(0, $menu->getDropdowns());
357357
}
@@ -382,7 +382,7 @@ public function testDropdownMenuItemsAreSortedByPriority()
382382
Routes::addRoute(new Route(new MarkdownPage('foo/bar', ['navigation.priority' => 2])));
383383
Routes::addRoute(new Route(new MarkdownPage('foo/baz', ['navigation.priority' => 3])));
384384

385-
$menu = $this->createMainNavigationMenu();
385+
$menu = $this->createNewMainNavigationMenu();
386386
$dropdowns = $menu->getDropdowns();
387387

388388
$this->assertSame(['Foo', 'Bar', 'Baz'], $dropdowns[0]->getItems()->pluck('label')->toArray());

0 commit comments

Comments
 (0)