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
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
11
|
|
|
* - (c) John MacFarlane |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace League\CommonMark\Reference; |
18
|
|
|
|
19
|
|
|
use League\CommonMark\Normalizer\TextNormalizer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A collection of references, indexed by label |
23
|
|
|
*/ |
24
|
|
|
final class ReferenceMap implements ReferenceMapInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TextNormalizer |
28
|
|
|
* |
29
|
|
|
* @psalm-readonly |
30
|
|
|
*/ |
31
|
|
|
private $normalizer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array<string, ReferenceInterface> |
35
|
|
|
* |
36
|
|
|
* @psalm-readonly-allow-private-mutation |
37
|
|
|
*/ |
38
|
|
|
private $references = []; |
39
|
|
|
|
40
|
2835 |
|
public function __construct() |
41
|
|
|
{ |
42
|
2835 |
|
$this->normalizer = new TextNormalizer(); |
43
|
2835 |
|
} |
44
|
|
|
|
45
|
288 |
|
public function add(ReferenceInterface $reference): void |
46
|
|
|
{ |
47
|
|
|
// Normalize the key |
48
|
288 |
|
$key = $this->normalizer->normalize($reference->getLabel()); |
49
|
|
|
// Store the reference |
50
|
288 |
|
$this->references[$key] = $reference; |
51
|
288 |
|
} |
52
|
|
|
|
53
|
246 |
|
public function contains(string $label): bool |
54
|
|
|
{ |
55
|
246 |
|
$label = $this->normalizer->normalize($label); |
56
|
|
|
|
57
|
246 |
|
return isset($this->references[$label]); |
58
|
|
|
} |
59
|
|
|
|
60
|
339 |
|
public function get(string $label): ?ReferenceInterface |
61
|
|
|
{ |
62
|
339 |
|
$label = $this->normalizer->normalize($label); |
63
|
|
|
|
64
|
339 |
|
if (! isset($this->references[$label])) { |
65
|
117 |
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
243 |
|
return $this->references[$label]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \Traversable<string, ReferenceInterface> |
|
|
|
|
73
|
|
|
*/ |
74
|
3 |
|
public function getIterator(): \Traversable |
75
|
|
|
{ |
76
|
3 |
|
foreach ($this->references as $normalizedLabel => $reference) { |
77
|
3 |
|
yield $normalizedLabel => $reference; |
78
|
|
|
} |
79
|
3 |
|
} |
80
|
|
|
|
81
|
6 |
|
public function count(): int |
82
|
|
|
{ |
83
|
6 |
|
return \count($this->references); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.