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