Passed
Push — master ( 37b230...58190a )
by Caen
02:59 queued 12s
created

MarkdownDocument::matter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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