| 148 |
lars |
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 <colinodell@gmail.com>
|
|
|
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 array $processorsByChar = [];
|
|
|
30 |
|
|
|
31 |
public function add(DelimiterProcessorInterface $processor): void
|
|
|
32 |
{
|
|
|
33 |
$opening = $processor->getOpeningCharacter();
|
|
|
34 |
$closing = $processor->getClosingCharacter();
|
|
|
35 |
|
|
|
36 |
if ($opening === $closing) {
|
|
|
37 |
$old = $this->processorsByChar[$opening] ?? null;
|
|
|
38 |
if ($old !== null && $old->getOpeningCharacter() === $old->getClosingCharacter()) {
|
|
|
39 |
$this->addStaggeredDelimiterProcessorForChar($opening, $old, $processor);
|
|
|
40 |
} else {
|
|
|
41 |
$this->addDelimiterProcessorForChar($opening, $processor);
|
|
|
42 |
}
|
|
|
43 |
} else {
|
|
|
44 |
$this->addDelimiterProcessorForChar($opening, $processor);
|
|
|
45 |
$this->addDelimiterProcessorForChar($closing, $processor);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function getDelimiterProcessor(string $char): ?DelimiterProcessorInterface
|
|
|
50 |
{
|
|
|
51 |
return $this->processorsByChar[$char] ?? null;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @return string[]
|
|
|
56 |
*/
|
|
|
57 |
public function getDelimiterCharacters(): array
|
|
|
58 |
{
|
|
|
59 |
return \array_keys($this->processorsByChar);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
private function addDelimiterProcessorForChar(string $delimiterChar, DelimiterProcessorInterface $processor): void
|
|
|
63 |
{
|
|
|
64 |
if (isset($this->processorsByChar[$delimiterChar])) {
|
|
|
65 |
throw new \InvalidArgumentException(\sprintf('Delim processor for character "%s" already exists', $processor->getOpeningCharacter()));
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
$this->processorsByChar[$delimiterChar] = $processor;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
private function addStaggeredDelimiterProcessorForChar(string $opening, DelimiterProcessorInterface $old, DelimiterProcessorInterface $new): void
|
|
|
72 |
{
|
|
|
73 |
if ($old instanceof StaggeredDelimiterProcessor) {
|
|
|
74 |
$s = $old;
|
|
|
75 |
} else {
|
|
|
76 |
$s = new StaggeredDelimiterProcessor($opening, $old);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$s->add($new);
|
|
|
80 |
$this->processorsByChar[$opening] = $s;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public function count(): int
|
|
|
84 |
{
|
|
|
85 |
return \count($this->processorsByChar);
|
|
|
86 |
}
|
|
|
87 |
}
|