Completed
Push — 1.5 ( c380b3...990492 )
by Colin
01:27
created

FootnoteRefParser::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 14
cp 0.8571
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0466
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
use League\CommonMark\Util\ConfigurationAwareInterface;
22
use League\CommonMark\Util\ConfigurationInterface;
23
24
final class FootnoteRefParser implements InlineParserInterface, ConfigurationAwareInterface
25
{
26
    /** @var ConfigurationInterface */
27
    private $config;
28
29 48
    public function getCharacters(): array
30
    {
31 48
        return ['['];
32
    }
33
34 36
    public function parse(InlineParserContext $inlineContext): bool
35
    {
36 36
        $container = $inlineContext->getContainer();
37 36
        $cursor = $inlineContext->getCursor();
38 36
        $nextChar = $cursor->peek();
39 36
        if ($nextChar !== '^') {
40 18
            return false;
41
        }
42
43 24
        $state = $cursor->saveState();
44
45 24
        $m = $cursor->match('#\[\^([^\]]+)\]#');
46 24
        if ($m !== null) {
47 24
            if (\preg_match('#\[\^([^\]]+)\]#', $m, $matches) > 0) {
48 24
                $container->appendChild(new FootnoteRef($this->createReference($matches[1])));
49
50 24
                return true;
51
            }
52
        }
53
54
        $cursor->restoreState($state);
55
56
        return false;
57
    }
58
59 24
    private function createReference(string $label): Reference
60
    {
61 24
        return new Reference(
62 24
            $label,
63 24
            '#' . $this->config->get('footnote/footnote_id_prefix', 'fn:') . $label,
64 8
            $label
65
        );
66
    }
67
68 48
    public function setConfiguration(ConfigurationInterface $config): void
69
    {
70 48
        $this->config = $config;
71 48
    }
72
}
73