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

FootnoteRefRenderer::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0009

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0009
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\Renderer;
16
17
use League\CommonMark\ElementRendererInterface;
18
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
19
use League\CommonMark\HtmlElement;
20
use League\CommonMark\Inline\Element\AbstractInline;
21
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
22
use League\CommonMark\Util\ConfigurationAwareInterface;
23
use League\CommonMark\Util\ConfigurationInterface;
24
25
final class FootnoteRefRenderer implements InlineRendererInterface, ConfigurationAwareInterface
26
{
27
    /** @var ConfigurationInterface */
28
    private $config;
29
30 48
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
31
    {
32 48
        if (!($inline instanceof FootnoteRef)) {
33
            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
34
        }
35
36 48
        $attrs = $inline->getData('attributes', []);
37 48
        $class = $attrs['class'] ?? $this->config->get('footnote/ref_class', 'footnote-ref');
38 48
        $idPrefix = $this->config->get('footnote/ref_id_prefix', 'fnref:');
39
40 48
        return new HtmlElement(
41 48
            'sup',
42
            [
43 48
                'id' => $idPrefix . \mb_strtolower($inline->getReference()->getLabel()),
44
            ],
45 48
            new HTMLElement(
46 48
                'a',
47
                [
48 48
                    'class' => $class,
49 48
                    'href'  => \mb_strtolower($inline->getReference()->getDestination()),
50 48
                    'role'  => 'doc-noteref',
51
                ],
52 48
                $inline->getReference()->getTitle()
53
            ),
54 48
            true
55
        );
56
    }
57
58 54
    public function setConfiguration(ConfigurationInterface $configuration)
59
    {
60 54
        $this->config = $configuration;
61 54
    }
62
}
63