1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
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
13declare(strict_types=1);
14
15namespace League\CommonMark\Extension\Footnote\Renderer;
16
17use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
18use League\CommonMark\Node\Node;
19use League\CommonMark\Renderer\ChildNodeRendererInterface;
20use League\CommonMark\Renderer\NodeRendererInterface;
21use League\CommonMark\Util\HtmlElement;
22use League\CommonMark\Xml\XmlNodeRendererInterface;
23use League\Config\ConfigurationAwareInterface;
24use League\Config\ConfigurationInterface;
25
26final class FootnoteRefRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
27{
28    private ConfigurationInterface $config;
29
30    /**
31     * @param FootnoteRef $node
32     *
33     * {@inheritDoc}
34     *
35     * @psalm-suppress MoreSpecificImplementedParamType
36     */
37    public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
38    {
39        FootnoteRef::assertInstanceOf($node);
40
41        $attrs = $node->data->getData('attributes');
42        $attrs->append('class', $this->config->get('footnote/ref_class'));
43        $attrs->set('href', \mb_strtolower($node->getReference()->getDestination(), 'UTF-8'));
44        $attrs->set('role', 'doc-noteref');
45
46        $idPrefix = $this->config->get('footnote/ref_id_prefix');
47
48        return new HtmlElement(
49            'sup',
50            [
51                'id' => $idPrefix . \mb_strtolower($node->getReference()->getLabel(), 'UTF-8'),
52            ],
53            new HtmlElement(
54                'a',
55                $attrs->export(),
56                $node->getReference()->getTitle()
57            ),
58            true
59        );
60    }
61
62    public function setConfiguration(ConfigurationInterface $configuration): void
63    {
64        $this->config = $configuration;
65    }
66
67    public function getXmlTagName(Node $node): string
68    {
69        return 'footnote_ref';
70    }
71
72    /**
73     * @param FootnoteRef $node
74     *
75     * @return array<string, scalar>
76     *
77     * @psalm-suppress MoreSpecificImplementedParamType
78     */
79    public function getXmlAttributes(Node $node): array
80    {
81        FootnoteRef::assertInstanceOf($node);
82
83        return [
84            'reference' => $node->getReference()->getLabel(),
85        ];
86    }
87}
88