Completed
Pull Request — master (#14)
by ARCANEDEV
04:51
created

Markdown::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMarkdown;
6
7
use Arcanedev\LaravelMarkdown\Contracts\{Markdown as MarkdownContract, Parser};
8
use Arcanedev\LaravelMarkdown\Exceptions\ParserBufferingException;
9
use Illuminate\Support\HtmlString;
10
11
/**
12
 * Class     Markdown
13
 *
14
 * @package  Arcanedev\LaravelMarkdown
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class Markdown implements MarkdownContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Properties
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Markdown parser.
26
     *
27
     * @var \Arcanedev\LaravelMarkdown\Contracts\Parser
28
     */
29
    protected $parser;
30
31
    /**
32
     * Indicates if the parser is currently buffering input.
33
     *
34
     * @var bool
35
     */
36
    protected $buffering = false;
37
38
    /* -----------------------------------------------------------------
39
     |  Constructor
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * Markdown constructor.
45
     *
46
     * @param  \Arcanedev\LaravelMarkdown\Contracts\Parser  $parser
47
     */
48 16
    public function __construct(Parser $parser)
49
    {
50 16
        $this->parser = $parser;
51 16
    }
52
53
    /* -----------------------------------------------------------------
54
     |  Main Methods
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59
     * Parse the given Markdown text into HTML.
60
     *
61
     * @param  string  $text
62
     *
63
     * @return \Illuminate\Support\HtmlString
64
     */
65 12
    public function parse(string $text): HtmlString
66
    {
67 12
        return new HtmlString(
68 12
            $this->parser->convertToHtml($text)
69
        );
70
    }
71
72
    /**
73
     * Start buffering output to be parsed.
74
     */
75 4
    public function begin(): void
76
    {
77 4
        $this->buffering = true;
78 4
        ob_start();
79 4
    }
80
81
    /**
82
     * Stop buffering output & return the parsed markdown string.
83
     *
84
     * @return \Illuminate\Support\HtmlString
85
     *
86
     * @throws \Arcanedev\LaravelMarkdown\Exceptions\ParserBufferingException
87
     */
88 6
    public function end(): HtmlString
89
    {
90 6
        if ($this->buffering === false) {
91 2
            throw new ParserBufferingException('Markdown buffering have not been started.');
92
        }
93
94 4
        $markdown        = ob_get_clean();
95 4
        $this->buffering = false;
96
97 4
        return $this->parse($markdown);
98
    }
99
}
100