Completed
Push — 1.5 ( c380b3...990492 )
by Colin
01:27
created

GatherFootnotesListener::getFootnotesContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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\Event;
16
17
use League\CommonMark\Block\Element\Document;
18
use League\CommonMark\Event\DocumentParsedEvent;
19
use League\CommonMark\Extension\Footnote\Node\Footnote;
20
use League\CommonMark\Extension\Footnote\Node\FootnoteBackref;
21
use League\CommonMark\Extension\Footnote\Node\FootnoteContainer;
22
use League\CommonMark\Reference\Reference;
23
use League\CommonMark\Util\ConfigurationAwareInterface;
24
use League\CommonMark\Util\ConfigurationInterface;
25
26
final class GatherFootnotesListener implements ConfigurationAwareInterface
27
{
28
    /** @var ConfigurationInterface */
29
    private $config;
30
31 48
    public function onDocumentParsed(DocumentParsedEvent $event): void
32
    {
33 48
        $document = $event->getDocument();
34 48
        $walker = $document->walker();
35
36 48
        $footnotes = [];
37 48
        while ($event = $walker->next()) {
38 48
            if (!$event->isEntering()) {
39 48
                continue;
40
            }
41
42 48
            $node = $event->getNode();
43 48
            if (!$node instanceof Footnote) {
44 48
                continue;
45
            }
46
47
            // Look for existing reference with footnote label
48 42
            $ref = $document->getReferenceMap()->getReference($node->getReference()->getLabel());
49 42
            if ($ref !== null) {
50
                // Use numeric title to get footnotes order
51 42
                $footnotes[\intval($ref->getTitle())] = $node;
52
            } else {
53
                // Footnote call is missing, append footnote at the end
54
                $footnotes[INF] = $node;
55
            }
56
57
            /*
58
             * Look for all footnote refs pointing to this footnote
59
             * and create each footnote backrefs.
60
             */
61 42
            $backrefs = $document->getData(
62 42
                '#' . $this->config->get('footnote/footnote_id_prefix', 'fn:') . $node->getReference()->getDestination(),
63 42
                []
64
            );
65
            /** @var Reference $backref */
66 42
            foreach ($backrefs as $backref) {
67 24
                $node->addBackref(new FootnoteBackref(new Reference(
68 24
                    $backref->getLabel(),
69 24
                    '#' . $this->config->get('footnote/ref_id_prefix', 'fnref:') . $backref->getLabel(),
70 24
                    $backref->getTitle()
71
                )));
72
            }
73
        }
74
75
        // Only add a footnote container if there are any
76 48
        if (\count($footnotes) === 0) {
77 6
            return;
78
        }
79
80 42
        $container = $this->getFootnotesContainer($document);
81
82 42
        \ksort($footnotes);
83 42
        foreach ($footnotes as $footnote) {
84 42
            $container->appendChild($footnote);
85
        }
86 42
    }
87
88 42
    private function getFootnotesContainer(Document $document): FootnoteContainer
89
    {
90 42
        $footnoteContainer = new FootnoteContainer();
91 42
        $document->appendChild($footnoteContainer);
92
93 42
        return $footnoteContainer;
94
    }
95
96 48
    public function setConfiguration(ConfigurationInterface $config): void
97
    {
98 48
        $this->config = $config;
99 48
    }
100
}
101