Passed
Push — latest ( 2a2bd2...5b2ad0 )
by Colin
02:22 queued 11s
created

MentionParser::getMatchDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Mention;
15
16
use League\CommonMark\Extension\Mention\Generator\CallbackGenerator;
17
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
18
use League\CommonMark\Extension\Mention\Generator\StringTemplateLinkGenerator;
19
use League\CommonMark\Parser\Inline\InlineParserInterface;
20
use League\CommonMark\Parser\Inline\InlineParserMatch;
21
use League\CommonMark\Parser\InlineParserContext;
22
23
final class MentionParser implements InlineParserInterface
24
{
25
    /**
26
     * @var string
27
     *
28
     * @psalm-readonly
29
     */
30
    private $name;
31
32
    /**
33
     * @var string
34
     *
35
     * @psalm-readonly
36
     */
37
    private $prefix;
38
39
    /**
40
     * @var string
41
     *
42
     * @psalm-readonly
43
     */
44
    private $identifierPattern;
45
46
    /**
47
     * @var MentionGeneratorInterface
48
     *
49
     * @psalm-readonly
50
     */
51
    private $mentionGenerator;
52
53 36
    public function __construct(string $name, string $prefix, string $identifierPattern, MentionGeneratorInterface $mentionGenerator)
54
    {
55 36
        $this->name              = $name;
56 36
        $this->prefix            = $prefix;
57 36
        $this->identifierPattern = $identifierPattern;
58 36
        $this->mentionGenerator  = $mentionGenerator;
59 36
    }
60
61 36
    public function getMatchDefinition(): InlineParserMatch
62
    {
63 36
        return InlineParserMatch::join(
64 36
            InlineParserMatch::string($this->prefix),
65 36
            InlineParserMatch::regex($this->identifierPattern)
66
        );
67
    }
68
69 30
    public function parse(InlineParserContext $inlineContext): bool
70
    {
71 30
        $cursor = $inlineContext->getCursor();
72
73
        // The prefix must not have any other characters immediately prior
74 30
        $previousChar = $cursor->peek(-1);
75 30
        if ($previousChar !== null && $previousChar !== ' ') {
76
            // peek() doesn't modify the cursor, so no need to restore state first
77 3
            return false;
78
        }
79
80 27
        [$prefix, $identifier] = $inlineContext->getSubMatches();
81
82 27
        $mention = $this->mentionGenerator->generateMention(new Mention($this->name, $prefix, $identifier));
83
84 27
        if ($mention === null) {
85 3
            return false;
86
        }
87
88 24
        $cursor->advanceBy($inlineContext->getFullMatchLength());
89 24
        $inlineContext->getContainer()->appendChild($mention);
90
91 24
        return true;
92
    }
93
94 3
    public static function createWithStringTemplate(string $name, string $prefix, string $mentionRegex, string $urlTemplate): MentionParser
95
    {
96 3
        return new self($name, $prefix, $mentionRegex, new StringTemplateLinkGenerator($urlTemplate));
97
    }
98
99 9
    public static function createWithCallback(string $name, string $prefix, string $mentionRegex, callable $callback): MentionParser
100
    {
101 9
        return new self($name, $prefix, $mentionRegex, new CallbackGenerator($callback));
102
    }
103
}
104