Completed
Push — master ( 006032...d9c179 )
by Fèvre
27s queued 12s
created

GithubPullRequestParser::getMatchDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace Xetaravel\Markdown\GithubPullRequest;
3
4
use League\CommonMark\Parser\Inline\InlineParserInterface;
5
use League\CommonMark\Parser\Inline\InlineParserMatch;
6
use League\CommonMark\Parser\InlineParserContext;
7
8
final class GithubPullRequestParser implements InlineParserInterface
9
{
10
    // Regex used to match Github pull request link.
11
    const REGEXP_PURLLREQUEST = '\bhttps?:\/\/github\.com\/(?<repo>[\w-]+\/[\w-]+)\/'.
12
        '(?<type>issues|pull)\/(?<issue>\d+)';
13
14
    public function getMatchDefinition(): InlineParserMatch
15
    {
16
        return InlineParserMatch::regex(self::REGEXP_PURLLREQUEST);
17
    }
18
19
    public function parse(InlineParserContext $inlineContext): bool
20
    {
21
        $route = $inlineContext->getFullMatch();
22
23
        // Push the cursor to the lenght of the full match.
24
        $inlineContext->getCursor()->advanceBy(\strlen($route));
25
26
        $content = "{$inlineContext->getMatches()[1]}#{$inlineContext->getMatches()[5]}";
27
28
29
        $inlineContext->getContainer()
30
            ->appendChild(new GithubPullRequest(
31
                $content,
32
                [
33
                    'attributes' => [
34
                        'href' => $route,
35
                        'data-toggle' => 'tooltip',
36
                        'title' => $content
37
                    ]
38
                ]
39
            ));
40
41
        return true;
42
    }
43
}
44