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

GithubCommitParser::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\GithubCommit;
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 GithubCommitParser implements InlineParserInterface
9
{
10
    // Regex used to match Github commit link.
11
    const REGEXP_COMMIT = '\bhttps?:\/\/github\.com\/(?<repo>[\w-]+\/[\w-]+)\/commit\/(?<commit>[0-9a-f]{7,40})';
12
13
    public function getMatchDefinition(): InlineParserMatch
14
    {
15
        return InlineParserMatch::regex(self::REGEXP_COMMIT);
16
    }
17
18
    public function parse(InlineParserContext $inlineContext): bool
19
    {
20
        $route = $inlineContext->getFullMatch();
21
22
        // Push the cursor to the lenght of the full match.
23
        $inlineContext->getCursor()->advanceBy(\strlen($route));
24
25
        $commit = substr($inlineContext->getMatches()[3], 0, 7);
26
27
        $content = "{$inlineContext->getMatches()[1]}@{$commit}";
28
29
30
        $inlineContext->getContainer()
31
            ->appendChild(new GithubCommit(
32
                $content,
33
                [
34
                    'attributes' => [
35
                        'href' => $route,
36
                        'data-toggle' => 'tooltip',
37
                        'title' => $content
38
                    ]
39
                ]
40
            ));
41
42
        return true;
43
    }
44
}
45