Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
HasDocumentationSidebarCategories | |
100.00% |
18 / 18 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
hasCategories | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getCategories | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getItemsInCategory | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
assembleCategories | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
setCategoryOfUncategorizedItems | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Concerns; |
4 | |
5 | use Hyde\Framework\Models\DocumentationSidebar; |
6 | use Illuminate\Support\Str; |
7 | |
8 | /** |
9 | * Extracts logic for the sidebar categories used in the SidebarService. |
10 | * |
11 | * @see \Hyde\Framework\Services\DocumentationSidebarService |
12 | */ |
13 | trait HasDocumentationSidebarCategories |
14 | { |
15 | protected array $categories = []; |
16 | |
17 | public function hasCategories(): bool |
18 | { |
19 | $this->assembleCategories(); |
20 | |
21 | return ! empty($this->categories); |
22 | } |
23 | |
24 | public function getCategories(): array |
25 | { |
26 | $this->assembleCategories(); |
27 | |
28 | return $this->categories; |
29 | } |
30 | |
31 | public function getItemsInCategory(string $category): DocumentationSidebar |
32 | { |
33 | return $this->sidebar->filter(function ($item) use ($category) { |
34 | return $item->category === Str::slug($category); |
35 | })->sortBy('priority')->values(); |
36 | } |
37 | |
38 | protected function assembleCategories(): void |
39 | { |
40 | foreach ($this->sidebar->sortItems() as $item) { |
41 | if (isset($item->category)) { |
42 | if (! in_array($item->category, $this->categories)) { |
43 | $this->categories[] = $item->category; |
44 | } |
45 | } |
46 | } |
47 | |
48 | if (! empty($this->categories)) { |
49 | $this->setCategoryOfUncategorizedItems(); |
50 | } |
51 | } |
52 | |
53 | protected function setCategoryOfUncategorizedItems(): void |
54 | { |
55 | foreach ($this->sidebar as $item) { |
56 | if (! isset($item->category)) { |
57 | $item->category = 'other'; |
58 | |
59 | if (! in_array('other', $this->categories)) { |
60 | $this->categories[] = 'other'; |
61 | } |
62 | } |
63 | } |
64 | } |
65 | } |