Completed
Push — master ( 103c69...214519 )
by Colin
35:28 queued 34:02
created

FootnoteRefRenderer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 41
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 27 2
A setConfiguration() 0 4 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\Renderer;
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\Node\Node;
21
use League\CommonMark\Renderer\ChildNodeRendererInterface;
22
use League\CommonMark\Renderer\NodeRendererInterface;
23
use League\CommonMark\Util\HtmlElement;
24
25
final class FootnoteRefRenderer implements NodeRendererInterface, ConfigurationAwareInterface
26
{
27
    /** @var ConfigurationInterface */
28
    private $config;
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 48
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
34
    {
35 48
        if (! ($node instanceof FootnoteRef)) {
36
            throw new \InvalidArgumentException('Incompatible node type: ' . \get_class($node));
37
        }
38
39 48
        $attrs    = $node->getData('attributes', []);
40 48
        $class    = $attrs['class'] ?? $this->config->get('footnote/ref_class', 'footnote-ref');
41 48
        $idPrefix = $this->config->get('footnote/ref_id_prefix', 'fnref:');
42
43 48
        return new HtmlElement(
44 48
            'sup',
45
            [
46 48
                'id' => $idPrefix . \mb_strtolower($node->getReference()->getLabel()),
47
            ],
48 48
            new HtmlElement(
49 48
                'a',
50
                [
51 48
                    'class' => $class,
52 48
                    'href'  => \mb_strtolower($node->getReference()->getDestination()),
53 48
                    'role'  => 'doc-noteref',
54
                ],
55 48
                $node->getReference()->getTitle()
56
            ),
57 48
            true
58
        );
59
    }
60
61 54
    public function setConfiguration(ConfigurationInterface $configuration): void
62
    {
63 54
        $this->config = $configuration;
64 54
    }
65
}
66