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
|
|
|
* Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java) |
14
|
|
|
* - (c) Atlassian Pty Ltd |
15
|
|
|
* |
16
|
|
|
* For the full copyright and license information, please view the LICENSE |
17
|
|
|
* file that was distributed with this source code. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace League\CommonMark\Delimiter\Processor; |
21
|
|
|
|
22
|
|
|
final class DelimiterProcessorCollection implements DelimiterProcessorCollectionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array<string,DelimiterProcessorInterface>|DelimiterProcessorInterface[] |
26
|
|
|
* |
27
|
|
|
* @psalm-readonly-allow-private-mutation |
28
|
|
|
*/ |
29
|
|
|
private $processorsByChar = []; |
30
|
|
|
|
31
|
2898 |
|
public function add(DelimiterProcessorInterface $processor): void |
32
|
|
|
{ |
33
|
2898 |
|
$opening = $processor->getOpeningCharacter(); |
34
|
2898 |
|
$closing = $processor->getClosingCharacter(); |
35
|
|
|
|
36
|
2898 |
|
if ($opening === $closing) { |
37
|
2892 |
|
$old = $this->processorsByChar[$opening] ?? null; |
38
|
2892 |
|
if ($old !== null && $old->getOpeningCharacter() === $old->getClosingCharacter()) { |
39
|
6 |
|
$this->addStaggeredDelimiterProcessorForChar($opening, $old, $processor); |
40
|
|
|
} else { |
41
|
2892 |
|
$this->addDelimiterProcessorForChar($opening, $processor); |
42
|
|
|
} |
43
|
|
|
} else { |
44
|
39 |
|
$this->addDelimiterProcessorForChar($opening, $processor); |
45
|
39 |
|
$this->addDelimiterProcessorForChar($closing, $processor); |
46
|
|
|
} |
47
|
2898 |
|
} |
48
|
|
|
|
49
|
1050 |
|
public function getDelimiterProcessor(string $char): ?DelimiterProcessorInterface |
50
|
|
|
{ |
51
|
1050 |
|
return $this->processorsByChar[$char] ?? null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string[] |
56
|
|
|
*/ |
57
|
2874 |
|
public function getDelimiterCharacters(): array |
58
|
|
|
{ |
59
|
2874 |
|
return \array_keys($this->processorsByChar); |
60
|
|
|
} |
61
|
|
|
|
62
|
2898 |
|
private function addDelimiterProcessorForChar(string $delimiterChar, DelimiterProcessorInterface $processor): void |
63
|
|
|
{ |
64
|
2898 |
|
if (isset($this->processorsByChar[$delimiterChar])) { |
65
|
3 |
|
throw new \InvalidArgumentException(\sprintf('Delim processor for character "%s" already exists', $processor->getOpeningCharacter())); |
66
|
|
|
} |
67
|
|
|
|
68
|
2898 |
|
$this->processorsByChar[$delimiterChar] = $processor; |
69
|
2898 |
|
} |
70
|
|
|
|
71
|
6 |
|
private function addStaggeredDelimiterProcessorForChar(string $opening, DelimiterProcessorInterface $old, DelimiterProcessorInterface $new): void |
72
|
|
|
{ |
73
|
6 |
|
if ($old instanceof StaggeredDelimiterProcessor) { |
74
|
|
|
$s = $old; |
75
|
|
|
} else { |
76
|
6 |
|
$s = new StaggeredDelimiterProcessor($opening, $old); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
$s->add($new); |
80
|
3 |
|
$this->processorsByChar[$opening] = $s; |
81
|
3 |
|
} |
82
|
|
|
|
83
|
2970 |
|
public function count(): int |
84
|
|
|
{ |
85
|
2970 |
|
return \count($this->processorsByChar); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|