Passed
Push — latest ( 981bad...ca3ef7 )
by Colin
02:16 queued 10s
created

HeadingPermalinkRenderer::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 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\Configuration\ConfigurationAwareInterface;
17
use League\CommonMark\Configuration\ConfigurationInterface;
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
23
/**
24
 * Renders the HeadingPermalink elements
25
 */
26
final class HeadingPermalinkRenderer implements NodeRendererInterface, ConfigurationAwareInterface
27
{
28
    public const DEFAULT_SYMBOL = '¶';
29
30
    /**
31
     * @var ConfigurationInterface
32
     *
33
     * @psalm-readonly-allow-private-mutation
34
     */
35
    private $config;
36
37 72
    public function setConfiguration(ConfigurationInterface $configuration): void
38
    {
39 72
        $this->config = $configuration;
40 72
    }
41
42
    /**
43
     * @param HeadingPermalink $node
44
     *
45
     * {@inheritdoc}
46
     *
47
     * @psalm-suppress MoreSpecificImplementedParamType
48
     */
49 66
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
50
    {
51 66
        if (! $node instanceof HeadingPermalink) {
52
            throw new \InvalidArgumentException('Incompatible node type: ' . \get_class($node));
53
        }
54
55 66
        $slug = $node->getSlug();
56
57 66
        $idPrefix = (string) $this->config->get('heading_permalink/id_prefix', 'user-content');
58 66
        if ($idPrefix !== '') {
59 63
            $idPrefix .= '-';
60
        }
61
62 66
        $attrs = $node->data->getData('attributes');
63 66
        $attrs->set('id', $idPrefix . $slug);
64 66
        $attrs->set('href', '#' . $slug);
65 66
        $attrs->set('name', $slug);
66 66
        $attrs->append('class', $this->config->get('heading_permalink/html_class', 'heading-permalink'));
67 66
        $attrs->set('aria-hidden', 'true');
68 66
        $attrs->set('title', $this->config->get('heading_permalink/title', 'Permalink'));
69
70 66
        $symbol = $this->config->get('heading_permalink/symbol', self::DEFAULT_SYMBOL);
71
        \assert(\is_string($symbol));
72
73 66
        return new HtmlElement('a', $attrs->export(), \htmlspecialchars($symbol), false);
74
    }
75
}
76