Passed
Push — master ( c8fc2a...b4ea14 )
by Caen
03:10 queued 12s
created

AbstractMarkdownPage::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Contracts\MarkdownDocumentContract;
6
use Hyde\Framework\Contracts\MarkdownPageContract;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\FrontMatter;
9
use Hyde\Framework\Models\Markdown;
10
11
/**
12
 * The base class for all Markdown-based Page Models.
13
 *
14
 * Normally, you would use the SourceFileParser to construct a MarkdownPage object.
15
 *
16
 * Extends the AbstractPage class to provide relevant
17
 * helpers for Markdown-based page model classes.
18
 *
19
 * @see \Hyde\Framework\Models\Pages\MarkdownPage
20
 * @see \Hyde\Framework\Models\Pages\MarkdownPost
21
 * @see \Hyde\Framework\Models\Pages\DocumentationPage
22
 * @see \Hyde\Framework\Concerns\AbstractPage
23
 * @see \Hyde\Framework\Testing\Feature\AbstractPageTest
24
 */
25
abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownDocumentContract, MarkdownPageContract
26
{
27
    public string $identifier;
28
    public Markdown $markdown;
29
30
    public static string $fileExtension = '.md';
31
32
    /** @interitDoc */
33
    public static function make(string $identifier = '', array $matter = [], string $body = ''): static
34
    {
35
        return new static($identifier, new FrontMatter($matter), new Markdown($body));
36
    }
37
38
    /** @interitDoc */
39
    public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null)
40
    {
41
        $this->identifier = $identifier;
42
        $this->matter = $matter ?? new FrontMatter();
43
        $this->markdown = $markdown ?? new Markdown();
44
45
        parent::__construct($this->identifier, $this->matter);
46
    }
47
48
    /** @inheritDoc */
49
    public function markdown(): Markdown
50
    {
51
        return $this->markdown;
52
    }
53
54
    /** @inheritDoc */
55
    public function compile(): string
56
    {
57
        return view($this->getBladeView())->with([
58
            'title' => $this->title,
59
            'markdown' => $this->markdown->compile(static::class),
60
        ])->render();
61
    }
62
63
    /** @inheritDoc */
64
    public function save(): static
65
    {
66
        file_put_contents(Hyde::path($this->getSourcePath()), ltrim("$this->matter\n$this->markdown"));
67
68
        return $this;
69
    }
70
}
71