Passed
Push — master ( df324d...7773d7 )
by Caen
03:06 queued 14s
created

MarkdownDocument::parseFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Contracts\MarkdownDocumentContract;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\Modules\Markdown\MarkdownFileParser;
8
9
/**
10
 * A MarkdownDocument is a simpler alternative to a MarkdownPage.
11
 *
12
 * It's an object that contains a parsed FrontMatter split from the body of the Markdown file.
13
 *
14
 * @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest
15
 */
16
class MarkdownDocument implements MarkdownDocumentContract, \Stringable
17
{
18
    public FrontMatter $matter;
19
    public Markdown $markdown;
20
21
    public function __construct(FrontMatter|array $matter = [], Markdown|string $body = '')
22
    {
23
        $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter);
0 ignored issues
show
introduced by
$matter is never a sub-type of Hyde\Framework\Models\FrontMatter.
Loading history...
24
        $this->markdown = $body instanceof Markdown ? $body : new Markdown($body);
25
    }
26
27
    public function __toString(): string
28
    {
29
        return $this->markdown;
30
    }
31
32
    public function matter(string $key = null, mixed $default = null): mixed
33
    {
34
        return $key ? $this->matter->get($key, $default) : $this->matter;
35
    }
36
37
    public function markdown(): Markdown
38
    {
39
        return $this->markdown;
40
    }
41
42
    public static function parse(string $localFilepath): static
43
    {
44
        return (new MarkdownFileParser(Hyde::path($localFilepath)))->get();
45
    }
46
}
47