Passed
Push — master ( bd3ba0...481116 )
by Caen
03:49 queued 13s
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
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Markdown\Models;
6
7
use Hyde\Framework\Actions\MarkdownFileParser;
8
use Hyde\Framework\Concerns\InteractsWithFrontMatter;
9
use Hyde\Markdown\Contracts\MarkdownDocumentContract;
10
use Stringable;
11
12
/**
13
 * A MarkdownDocument is a simpler alternative to a MarkdownPage.
14
 *
15
 * It's an object that contains a parsed FrontMatter split from the body of the Markdown file.
16
 *
17
 * @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest
18
 */
19
class MarkdownDocument implements MarkdownDocumentContract, Stringable
20
{
21
    use InteractsWithFrontMatter;
22
23
    public FrontMatter $matter;
24
    public Markdown $markdown;
25
26
    public function __construct(FrontMatter|array $matter = [], Markdown|string $body = '')
27
    {
28
        $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter);
0 ignored issues
show
introduced by
$matter is never a sub-type of Hyde\Markdown\Models\FrontMatter.
Loading history...
29
        $this->markdown = $body instanceof Markdown ? $body : new Markdown($body);
30
    }
31
32
    public function __toString(): string
33
    {
34
        return $this->markdown->__toString();
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($localFilepath))->get();
45
    }
46
}
47