ReplyParser   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 21 1
A getMatchDefinition() 0 3 1
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