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

FootnoteRefRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
dl 0
loc 60
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getXmlAttributes() 0 6 1
A getXmlTagName() 0 3 1
A setConfiguration() 0 3 1
A render() 0 22 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\FootnoteRef;
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 FootnoteRefRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
27
{
28
    /** @var ConfigurationInterface */
29
    private $config;
30
31
    /**
32
     * @param FootnoteRef $node
33
     *
34
     * {@inheritDoc}
35
     *
36
     * @psalm-suppress MoreSpecificImplementedParamType
37
     */
38 108
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
39
    {
40 108
        FootnoteRef::assertInstanceOf($node);
41
42 108
        $attrs = $node->data->getData('attributes');
43 108
        $attrs->append('class', $this->config->get('footnote/ref_class'));
44 108
        $attrs->set('href', \mb_strtolower($node->getReference()->getDestination()));
45 108
        $attrs->set('role', 'doc-noteref');
46
47 108
        $idPrefix = $this->config->get('footnote/ref_id_prefix');
48
49 108
        return new HtmlElement(
50 108
            'sup',
51
            [
52 108
                'id' => $idPrefix . \mb_strtolower($node->getReference()->getLabel()),
53
            ],
54 108
            new HtmlElement(
55 108
                'a',
56 108
                $attrs->export(),
57 108
                $node->getReference()->getTitle()
58
            ),
59 108
            true
60
        );
61
    }
62
63 147
    public function setConfiguration(ConfigurationInterface $configuration): void
64
    {
65 147
        $this->config = $configuration;
66 147
    }
67
68 27
    public function getXmlTagName(Node $node): string
69
    {
70 27
        return 'footnote_ref';
71
    }
72
73
    /**
74
     * @param FootnoteRef $node
75
     *
76
     * @return array<string, scalar>
77
     *
78
     * @psalm-suppress MoreSpecificImplementedParamType
79
     */
80 27
    public function getXmlAttributes(Node $node): array
81
    {
82 27
        FootnoteRef::assertInstanceOf($node);
83
84
        return [
85 27
            'reference' => $node->getReference()->getLabel(),
86
        ];
87
    }
88
}
89