-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathDocumentationMenuItem.php
More file actions
92 lines (78 loc) · 2.77 KB
/
DocumentationMenuItem.php
File metadata and controls
92 lines (78 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace App\Documentation;
use Nette\Utils\FileSystem;
use Webmozart\Assert\Assert;
final readonly class DocumentationMenuItem
{
/**
* @param string $href The URL path for the documentation menu item. Must be a non-empty string starting with '/', 'http://', or 'https://'
* @param string $label The display text for the menu item. Must be a non-empty string
* @param non-empty-string|null $slug The unique identifier for the documentation file. If null, indicates a menu item without content
* @param bool $isNew Whether this menu item represents new documentation
*/
public function __construct(
private string $href,
private string $label,
private ?string $slug,
private bool $isNew = false,
) {
Assert::notEmpty($href, 'Documentation href cannot be empty');
Assert::true(
str_starts_with($href, '/') || str_starts_with($href, 'http://') || str_starts_with($href, 'https://'),
'Documentation href must start with a forward slash, http://, or https://'
);
Assert::notEmpty($label, 'Documentation label cannot be empty');
}
public function getHref(): string
{
return $this->href;
}
public function getLabel(): string
{
return $this->label;
}
public function isNew(): bool
{
return $this->isNew;
}
/**
* @return non-empty-string|null
*/
public function getSlug(): ?string
{
return $this->slug;
}
/**
* Checks if the documentation file exists for this menu item.
* Throws an exception if the file does not exist when a slug is provided.
*
* @return bool True if the documentation file exists, false if slug is null
* @throws \Webmozart\Assert\InvalidArgumentException If the file does not exist when slug is provided
*/
public function hasDocumentation(): bool
{
if ($this->slug === null) {
return false;
}
$documentationFilePath = $this->getDocumentationFilePath();
Assert::fileExists($documentationFilePath, sprintf('Documentation file must exist at "%s"', $documentationFilePath));
return true;
}
public function getMarkdownContents(): string
{
Assert::notNull($this->slug);
$documentationFilePath = $this->getDocumentationFilePath();
Assert::fileExists($documentationFilePath);
return FileSystem::read($documentationFilePath);
}
/**
* Gets the full path to the documentation file.
*
* @return string The absolute path to the documentation file
*/
private function getDocumentationFilePath(): string
{
return __DIR__ . '/../../resources/docs/' . $this->slug . '.md';
}
}