Completed
Pull Request — master (#14)
by ARCANEDEV
02:58 queued 45s
created

AbstractParser::end()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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