Completed
Push — master ( da0d4f...97e8b3 )
by Colin
03:00
created

ReferenceMap::getReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Reference;
16
17
/**
18
 * A collection of references, indexed by label
19
 */
20
class ReferenceMap
21
{
22
    /**
23
     * @var Reference[]
24
     */
25
    protected $references = [];
26
27
    /**
28
     * @param Reference $reference
29
     *
30
     * @return $this
31
     */
32 243
    public function addReference(Reference $reference)
33
    {
34 243
        $key = Reference::normalizeReference($reference->getLabel());
35 243
        $this->references[$key] = $reference;
36
37 243
        return $this;
38
    }
39
40
    /**
41
     * @param string $label
42
     *
43
     * @return bool
44
     */
45 240
    public function contains(string $label): bool
46
    {
47 240
        $label = Reference::normalizeReference($label);
48
49 240
        return isset($this->references[$label]);
50
    }
51
52
    /**
53
     * @param string $label
54
     *
55
     * @return Reference|null
56
     */
57 294
    public function getReference(string $label): ?Reference
58
    {
59 294
        $label = Reference::normalizeReference($label);
60
61 294
        if (!isset($this->references[$label])) {
62 111
            return null;
63
        }
64
65 204
        return $this->references[$label];
66
    }
67
68
    /**
69
     * Lists all registered references.
70
     *
71
     * @return Reference[]
72
     */
73 6
    public function listReferences(): iterable
74
    {
75 6
        return \array_values($this->references);
76
    }
77
}
78