Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
DocumentationSidebarItem
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 findPriorityInConfig
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isHidden
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseFromFile
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 normalizeCategoryKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Hyde\Framework\Models;
4
5use Hyde\Framework\Hyde;
6use Illuminate\Support\Str;
7use Spatie\YamlFrontMatter\YamlFrontMatter;
8
9/**
10 * Object containing information for a sidebar item.
11 *
12 * @see \Tests\Feature\Services\DocumentationSidebarServiceTest
13 */
14class DocumentationSidebarItem
15{
16    public string $label;
17    public string $destination;
18    public int $priority;
19    public bool $hidden = false;
20    public ?string $category = null;
21
22    public function __construct(string $label, string $destination, ?int $priority = null, ?string $category = null, bool $hidden = false)
23    {
24        $this->label = $label;
25        $this->destination = $destination;
26        $this->priority = $priority ?? $this->findPriorityInConfig($destination);
27        $this->category = $this->normalizeCategoryKey($category);
28        $this->hidden = $hidden;
29    }
30
31    protected function findPriorityInConfig(string $slug): int
32    {
33        $orderIndexArray = config('docs.sidebar_order', []);
34
35        if (! in_array($slug, $orderIndexArray)) {
36            return 500;
37        }
38
39        return array_search($slug, $orderIndexArray) + 250;
40
41        // Adding 250 makes so that pages with a front matter priority that is lower
42        // can be shown first. It's lower than the fallback of 500 so that they
43        // still come first. This is all to make it easier to mix priorities.
44    }
45
46    public function isHidden(): bool
47    {
48        return $this->hidden;
49    }
50
51    public static function parseFromFile(string $documentationPageSlug): static
52    {
53        $matter = YamlFrontMatter::markdownCompatibleParse(
54            file_get_contents(Hyde::getDocumentationPagePath('/'.$documentationPageSlug.'.md'))
55        )->matter();
56
57        return new static(
58            $matter['label'] ?? Hyde::titleFromSlug($documentationPageSlug),
59            $documentationPageSlug,
60            $matter['priority'] ?? null,
61            $matter['category'] ?? null,
62            $matter['hidden'] ?? false
63        );
64    }
65
66    protected function normalizeCategoryKey(?string $category): ?string
67    {
68        return empty($category) ? null : Str::slug($category);
69    }
70}