Passed
Push — render-xml ( 61b7f8 )
by Colin
02:12
created

HeadingPermalinkRenderer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 23 2
A getXmlTagName() 0 3 1
A getXmlAttributes() 0 6 1
A setConfiguration() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\HeadingPermalink;
15
16
use League\CommonMark\Node\Node;
17
use League\CommonMark\Renderer\ChildNodeRendererInterface;
18
use League\CommonMark\Renderer\NodeRendererInterface;
19
use League\CommonMark\Util\HtmlElement;
20
use League\CommonMark\Xml\XmlNodeRendererInterface;
21
use League\Config\ConfigurationAwareInterface;
22
use League\Config\ConfigurationInterface;
23
24
/**
25
 * Renders the HeadingPermalink elements
26
 */
27
final class HeadingPermalinkRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
28
{
29
    public const DEFAULT_SYMBOL = '¶';
30
31
    /**
32
     * @var ConfigurationInterface
33
     *
34
     * @psalm-readonly-allow-private-mutation
35
     */
36
    private $config;
37
38 120
    public function setConfiguration(ConfigurationInterface $configuration): void
39
    {
40 120
        $this->config = $configuration;
41 120
    }
42
43
    /**
44
     * @param HeadingPermalink $node
45
     *
46
     * {@inheritDoc}
47
     *
48
     * @psalm-suppress MoreSpecificImplementedParamType
49
     */
50 72
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
51
    {
52 72
        HeadingPermalink::assertInstanceOf($node);
53
54 72
        $slug = $node->getSlug();
55
56 72
        $idPrefix = (string) $this->config->get('heading_permalink/id_prefix');
57 72
        if ($idPrefix !== '') {
58 69
            $idPrefix .= '-';
59
        }
60
61 72
        $attrs = $node->data->getData('attributes');
62 72
        $attrs->set('id', $idPrefix . $slug);
63 72
        $attrs->set('href', '#' . $slug);
64 72
        $attrs->set('name', $slug);
65 72
        $attrs->append('class', $this->config->get('heading_permalink/html_class'));
66 72
        $attrs->set('aria-hidden', 'true');
67 72
        $attrs->set('title', $this->config->get('heading_permalink/title'));
68
69 72
        $symbol = $this->config->get('heading_permalink/symbol');
70
        \assert(\is_string($symbol));
71
72 72
        return new HtmlElement('a', $attrs->export(), \htmlspecialchars($symbol), false);
73
    }
74
75 42
    public function getXmlTagName(Node $node): string
76
    {
77 42
        return 'heading_permalink';
78
    }
79
80
    /**
81
     * @param HeadingPermalink $node
82
     *
83
     * @return array<string, scalar>
84
     *
85
     * @psalm-suppress MoreSpecificImplementedParamType
86
     */
87 42
    public function getXmlAttributes(Node $node): array
88
    {
89 42
        HeadingPermalink::assertInstanceOf($node);
90
91
        return [
92 42
            'slug' => $node->getSlug(),
93
        ];
94
    }
95
}
96