Passed
Push — master ( 7dcd3c...6c7933 )
by Caen
03:11 queued 12s
created

Markdown::toHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Markdown\Models;
6
7
use Hyde\Framework\Services\MarkdownService;
8
use Hyde\Markdown\MarkdownConverter;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Contracts\Support\Htmlable;
11
use Illuminate\Support\HtmlString;
12
use Stringable;
13
14
/**
15
 * A simple object representation of a Markdown file, with helpful methods to interact with it.
16
 *
17
 * @see \Hyde\Framework\Testing\Unit\MarkdownDocumentTest
18
 */
19
class Markdown implements Arrayable, Stringable, Htmlable
20
{
21
    public string $body;
22
23
    /**
24
     * Create a new Markdown object from a string.
25
     */
26
    public function __construct(string $body = '')
27
    {
28
        $this->body = $body;
29
    }
30
31
    /**
32
     * Get the source Markdown body.
33
     */
34
    public function __toString(): string
35
    {
36
        return $this->body;
37
    }
38
39
    /**
40
     * Get the source Markdown body.
41
     */
42
    public function body(): string
43
    {
44
        return $this->body;
45
    }
46
47
    /**
48
     * Compile the Markdown body to a string of HTML.
49
     *
50
     * If the Markdown being compiled is from a page model, supply
51
     * model's class name here so the dynamic parser can be used.
52
     *
53
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $sourceModel
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>|null.
Loading history...
54
     */
55
    public function compile(?string $sourceModel = null): string
56
    {
57
        return static::render($this->body, $sourceModel);
58
    }
59
60
    /**
61
     * Same as Markdown::compile(), but returns an HtmlString object.
62
     */
63
    public function toHtml(?string $sourceModel = null): HtmlString
64
    {
65
        return new HtmlString($this->compile($sourceModel));
66
    }
67
68
    /**
69
     * Get the Markdown document body as an array of lines.
70
     *
71
     * @return string[]
72
     */
73
    public function toArray(): array
74
    {
75
        return explode("\n", $this->body);
0 ignored issues
show
Bug Best Practice introduced by
The expression return explode(' ', $this->body) returns the type string[] which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of Illuminate\Contracts\Support\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
76
    }
77
78
    /**
79
     * Parse a Markdown file into a new Markdown object.
80
     */
81
    public static function fromFile(string $localFilepath): static
82
    {
83
        return MarkdownDocument::parse($localFilepath)->markdown();
84
    }
85
86
    /**
87
     * Render a Markdown string into HTML.
88
     *
89
     * If a source model is provided, the Markdown will be converted using the dynamic MarkdownService,
90
     * otherwise, the pre-configured singleton from the service container will be used instead.
91
     *
92
     * @return string $html
93
     */
94
    public static function render(string $markdown, ?string $sourceModel = null): string
95
    {
96
        return $sourceModel !== null
97
            ? (new MarkdownService($markdown, $sourceModel))->parse()
98
            : (string) app(MarkdownConverter::class)->convert($markdown);
99
    }
100
}
101