Skip to content

Commit

Permalink
Create the sidebar models
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 7, 2022
1 parent cb3c337 commit fbcae7c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Models/DocumentationSidebar.php
@@ -0,0 +1,36 @@
<?php

namespace Hyde\Framework\Models;

use Hyde\Framework\Contracts\DocumentationSidebarContract;
use Illuminate\Support\Collection;

/**
* The documentation sidebar, containing all the sidebar items.
*
* Extends the Illuminate\Support\Collection class and has helper
* methods to fluently add DocumentationSidebarItems to the
* collection using method chaining.
*
* @see \Tests\Feature\Services\DocumentationSidebarServiceTest
*/
class DocumentationSidebar extends Collection implements DocumentationSidebarContract
{
public function addItem(DocumentationSidebarItem $item): self
{
$this->push($item);

return $this;
}

public function sortItems(): self
{
return $this->sortBy('priority')
->values(); // Reset the keys to consecutively numbered indexes:
}

public function getCollection(): self
{
return $this;
}
}
49 changes: 49 additions & 0 deletions src/Models/DocumentationSidebarItem.php
@@ -0,0 +1,49 @@
<?php

namespace Hyde\Framework\Models;

use Hyde\Framework\Hyde;
use Spatie\YamlFrontMatter\YamlFrontMatter;

/**
* Object containing information for a sidebar item.
*
* @see \Tests\Feature\Services\DocumentationSidebarServiceTest
*/
class DocumentationSidebarItem
{
public string $label;
public string $destination;
public int $priority;

public function __construct(string $label, string $destination, ?int $priority = null)
{
$this->label = $label;
$this->destination = $destination;
$this->priority = $priority ?? $this->findPriorityInConfig($destination);
}

protected function findPriorityInConfig(string $slug): int
{
$orderIndexArray = config('hyde.documentationPageOrder', []);

if (! in_array($slug, $orderIndexArray)) {
return 500;
}

return array_search($slug, $orderIndexArray); // + 250?
}

public static function parseFromFile(string $documentationPageSlug): static
{
$matter = YamlFrontMatter::markdownCompatibleParse(
file_get_contents(Hyde::path('_docs/' . $documentationPageSlug . '.md'))
)->matter();

return new static(
$matter['label'] ?? Hyde::titleFromSlug($documentationPageSlug),
$documentationPageSlug,
$matter['priority'] ?? null
);
}
}

0 comments on commit fbcae7c

Please sign in to comment.