Passed
Push — master ( f703f5...72f565 )
by Caen
03:04 queued 14s
created

AbstractMarkdownPage::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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