Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
29.41% |
5 / 17 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| NavLink | |
29.41% |
5 / 17 |
|
0.00% |
0 / 5 |
30.51 | |
0.00% |
0 / 1 |
| __construct | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| slug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| children | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| active | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| icon | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray; |
| 6 | |
| 7 | use Cosray\Exception\RuntimeException; |
| 8 | use 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 | */ |
| 14 | final class NavLink implements NavigationItem |
| 15 | { |
| 16 | public readonly NavMeta $meta; |
| 17 | |
| 18 | public function __construct( |
| 19 | string $label, |
| 20 | public readonly string $url, |
| 21 | public readonly ?string $activePrefix = null, |
| 22 | ) { |
| 23 | $label = trim($label); |
| 24 | |
| 25 | if ($label === '') { |
| 26 | throw new RuntimeException('Nav link labels must not be empty'); |
| 27 | } |
| 28 | |
| 29 | $this->meta = new NavMeta($label); |
| 30 | } |
| 31 | |
| 32 | #[Override] |
| 33 | public function slug(): ?string |
| 34 | { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | /** @return list<NavigationItem> */ |
| 39 | #[Override] |
| 40 | public function children(): array |
| 41 | { |
| 42 | return []; |
| 43 | } |
| 44 | |
| 45 | public function active(string $currentPath): bool |
| 46 | { |
| 47 | if ($this->activePrefix !== null) { |
| 48 | return str_starts_with($currentPath, $this->activePrefix); |
| 49 | } |
| 50 | |
| 51 | return $currentPath === $this->url; |
| 52 | } |
| 53 | |
| 54 | /** @param array<array-key, mixed> $args */ |
| 55 | public function icon(string $id, array $args = []): static |
| 56 | { |
| 57 | $id = trim($id); |
| 58 | |
| 59 | if ($id === '') { |
| 60 | throw new RuntimeException('Nav link icon ids must not be empty'); |
| 61 | } |
| 62 | |
| 63 | $this->meta->icon = [ |
| 64 | 'id' => $id, |
| 65 | 'args' => $args, |
| 66 | ]; |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | } |