Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.29% covered (danger)
35.29%
6 / 17
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
NavLink
35.29% covered (danger)
35.29%
6 / 17
20.00% covered (danger)
20.00%
1 / 5
25.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 children
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 active
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 icon
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray;
6
7use Cosray\Exception\RuntimeException;
8use Override;
9
10/**
11 * Navigation entry pointing at an arbitrary URL — the nav item type
12 * for plugin apps with their own panel pages.
13 */
14final class NavLink implements NavigationItem
15{
16    #[Override]
17    public readonly NavMeta $meta;
18
19    public function __construct(
20        string $label,
21        public readonly string $url,
22        public readonly ?string $activePrefix = null,
23    ) {
24        $label = trim($label);
25
26        if ($label === '') {
27            throw new RuntimeException('Nav link labels must not be empty');
28        }
29
30        $this->meta = new NavMeta($label);
31    }
32
33    #[Override]
34    public function slug(): ?string
35    {
36        return null;
37    }
38
39    /** @return list<NavigationItem> */
40    #[Override]
41    public function children(): array
42    {
43        return [];
44    }
45
46    public function active(string $currentPath): bool
47    {
48        if ($this->activePrefix !== null) {
49            return str_starts_with($currentPath, $this->activePrefix);
50        }
51
52        return $currentPath === $this->url;
53    }
54
55    /** @param array<array-key, mixed> $args */
56    public function icon(string $id, array $args = []): static
57    {
58        $id = trim($id);
59
60        if ($id === '') {
61            throw new RuntimeException('Nav link icon ids must not be empty');
62        }
63
64        $this->meta->icon = [
65            'id' => $id,
66            'args' => $args,
67        ];
68
69        return $this;
70    }
71}