Passed
Push — main ( 989696...2152d5 )
by Colin
05:14 queued 02:09
created

FootnoteRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.9332
c 1
b 0
f 0
cc 1
nc 1
nop 2
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\Renderer;
16
17
use League\CommonMark\Extension\Footnote\Node\Footnote;
18
use League\CommonMark\Node\Node;
19
use League\CommonMark\Renderer\ChildNodeRendererInterface;
20
use League\CommonMark\Renderer\NodeRendererInterface;
21
use League\CommonMark\Util\HtmlElement;
22
use League\CommonMark\Xml\XmlNodeRendererInterface;
23
use League\Config\ConfigurationAwareInterface;
24
use League\Config\ConfigurationInterface;
25
26
final class FootnoteRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
27
{
28
    /** @var ConfigurationInterface */
29
    private $config;
30
31
    /**
32
     * @param Footnote $node
33
     *
34
     * {@inheritDoc}
35
     *
36
     * @psalm-suppress MoreSpecificImplementedParamType
37
     */
38 108
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
39
    {
40 108
        Footnote::assertInstanceOf($node);
41
42 108
        $attrs = $node->data->getData('attributes');
43
44 108
        $attrs->append('class', $this->config->get('footnote/footnote_class'));
45 108
        $attrs->set('id', $this->config->get('footnote/footnote_id_prefix') . \mb_strtolower($node->getReference()->getLabel()));
46 108
        $attrs->set('role', 'doc-endnote');
47
48 108
        return new HtmlElement(
49 108
            'li',
50 108
            $attrs->export(),
51 108
            $childRenderer->renderNodes($node->children()),
52 108
            true
53
        );
54
    }
55
56 147
    public function setConfiguration(ConfigurationInterface $configuration): void
57
    {
58 147
        $this->config = $configuration;
59 147
    }
60
61 27
    public function getXmlTagName(Node $node): string
62
    {
63 27
        return 'footnote';
64
    }
65
66
    /**
67
     * @param Footnote $node
68
     *
69
     * @return array<string, scalar>
70
     *
71
     * @psalm-suppress MoreSpecificImplementedParamType
72
     */
73 27
    public function getXmlAttributes(Node $node): array
74
    {
75 27
        Footnote::assertInstanceOf($node);
76
77
        return [
78 27
            'reference' => $node->getReference()->getLabel(),
79
        ];
80
    }
81
}
82