ReplyParser::parse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Markdown\Reply;
6
7
use League\CommonMark\Parser\Inline\InlineParserInterface;
8
use League\CommonMark\Parser\Inline\InlineParserMatch;
9
use League\CommonMark\Parser\InlineParserContext;
10
11
use function mb_strlen;
12
13
final class ReplyParser implements InlineParserInterface
14
{
15
    // Regex used to match Replies
16
    public const REGEXP_REPLY = '\@(?<user>[\w]{4,20})\#(?<post>[0-9]{1,16})';
17
18
    public function getMatchDefinition(): InlineParserMatch
19
    {
20
        return InlineParserMatch::regex(self::REGEXP_REPLY);
21
    }
22
23
    public function parse(InlineParserContext $inlineContext): bool
24
    {
25
        $reply = $inlineContext->getFullMatch();
26
        $route = route('discuss.post.show', ['id' => $inlineContext->getMatches()[3]]);
27
28
        // Push the cursor to the lenght of the full match.
29
        $inlineContext->getCursor()->advanceBy(mb_strlen($reply));
30
31
        $inlineContext->getContainer()
32
            ->appendChild(new Reply(
33
                $inlineContext->getMatches()[0],
34
                [
35
                    'attributes' => [
36
                        'href' => $route,
37
                        'class' => 'link link-hover link-primary',
38
                        'title' => 'View the answer of ' . $inlineContext->getMatches()[2]
39
                    ]
40
                ]
41
            ));
42
43
        return true;
44
    }
45
}
46