Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

NavigationGroup::normalizeGroupKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use Illuminate\Support\Str;
8
use Hyde\Pages\DocumentationPage;
9
10
use function min;
11
12
/**
13
 * Abstraction for a grouped navigation menu item, like a dropdown or a sidebar group.
14
 */
15
class NavigationGroup extends NavigationMenu
16
{
17
    protected string $label;
18
    protected int $priority;
19
20
    public function __construct(string $label, array $items = [], int $priority = NavigationMenu::LAST)
21
    {
22
        parent::__construct($items);
23
24
        $this->label = $label;
25
        $this->priority = $priority;
26
    }
27
28
    public static function create(string $label, array $items = [], int $priority = NavigationMenu::LAST): static
29
    {
30
        return new static($label, $items, $priority);
31
    }
32
33
    public function getLabel(): string
34
    {
35
        return $this->label;
36
    }
37
38
    public function getGroupKey(): string
39
    {
40
        return Str::slug($this->label);
41
    }
42
43
    public function getPriority(): int
44
    {
45
        if ($this->containsOnlyDocumentationPages()) {
46
            // For sidebar groups, we use the priority of the lowest priority child, unless the dropdown instance itself has a lower priority.
47
            return (int) min($this->priority, $this->getItems()->min(fn (NavigationItem $item): int => $item->getPriority()));
48
        }
49
50
        return $this->priority;
51
    }
52
53
    protected function containsOnlyDocumentationPages(): bool
54
    {
55
        return count($this->getItems()) && $this->getItems()->every(function (NavigationItem $item): bool {
56
            return $item->getPage() instanceof DocumentationPage;
57
        });
58
    }
59
60
    /** @experimental This method is subject to change before its release. */
61
    public static function normalizeGroupKey(string $group): string
62
    {
63
        return Str::slug($group);
64
    }
65
}
66