Completed
Push — 1.5 ( f40f61...09e907 )
by Colin
02:28
created

FootnoteRefParser::getCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) Rezo Zero / Ambroise Maupate
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Footnote\Parser;
16
17
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
18
use League\CommonMark\Inline\Parser\InlineParserInterface;
19
use League\CommonMark\InlineParserContext;
20
use League\CommonMark\Reference\Reference;
21
22
final class FootnoteRefParser implements InlineParserInterface
23
{
24 42
    public function getCharacters(): array
25
    {
26 42
        return ['['];
27
    }
28
29 30
    public function parse(InlineParserContext $inlineContext): bool
30
    {
31 30
        $container = $inlineContext->getContainer();
32 30
        $cursor = $inlineContext->getCursor();
33 30
        $nextChar = $cursor->peek();
34 30
        if ($nextChar !== '^') {
35 18
            return false;
36
        }
37
38 18
        $state = $cursor->saveState();
39
40 18
        $m = $cursor->match('#\[\^([^\]]+)\]#');
41 18
        if ($m !== null) {
42 18
            if (\preg_match('#\[\^([^\]]+)\]#', $m, $matches) > 0) {
43 18
                $container->appendChild(new FootnoteRef($this->createReference($matches[1])));
44
45 18
                return true;
46
            }
47
        }
48
49
        $cursor->restoreState($state);
50
51
        return false;
52
    }
53
54 18
    private function createReference(string $label): Reference
55
    {
56 18
        return new Reference($label, '#fn:' . $label, $label);
57
    }
58
}
59