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