Passed
Push — latest ( ba39d8...ca9086 )
by Colin
08:23
created

FootnoteRefParser::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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