Completed
Push — master ( 990cee...3d5add )
by Colin
13s queued 11s
created

StaggeredDelimiterProcessor::findProcessor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 4
cts 6
cp 0.6667
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.3332
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
9
 *  - (c) Atlassian Pty Ltd
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\Delimiter\Processor;
16
17
use League\CommonMark\Delimiter\Delimiter;
18
use League\CommonMark\Inline\Element\Text;
19
20
/**
21
 * An implementation of DelimiterProcessorInterface that dispatches all calls to two or more other DelimiterProcessors
22
 * depending on the length of the delimiter run. All child DelimiterProcessors must have different minimum
23
 * lengths. A given delimiter run is dispatched to the child with the largest acceptable minimum length. If no
24
 * child is applicable, the one with the largest minimum length is chosen.
25
 */
26
final class StaggeredDelimiterProcessor implements DelimiterProcessorInterface
27
{
28
    private $delimiterChar;
29
30
    private $minLength = 0;
31
32
    /** @var array<int, DelimiterProcessorInterface>|DelimiterProcessorInterface[] */
33
    private $processors = []; // keyed by minLength in reverse order
34
35 6
    public function __construct(string $char, DelimiterProcessorInterface $processor)
36
    {
37 6
        $this->delimiterChar = $char;
38 6
        $this->add($processor);
39 6
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function getOpeningCharacter(): string
45
    {
46 3
        return $this->delimiterChar;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 3
    public function getClosingCharacter(): string
53
    {
54 3
        return $this->delimiterChar;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    public function getMinLength(): int
61
    {
62 3
        return $this->minLength;
63
    }
64
65
    /**
66
     * Adds the given processor to this staggered delimiter processor
67
     *
68
     * @param DelimiterProcessorInterface $processor
69
     */
70 6
    public function add(DelimiterProcessorInterface $processor)
71
    {
72 6
        $len = $processor->getMinLength();
73
74 6
        if (isset($this->processors[$len])) {
75 3
            throw new \InvalidArgumentException(sprintf('Cannot add two delimiter processors for char "%s" and minimum length %d', $this->delimiterChar, $len));
76
        }
77
78 6
        $this->processors[$len] = $processor;
79 6
        krsort($this->processors);
80
81 6
        $this->minLength = min($this->minLength, $len);
82 6
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 3
    public function getDelimiterUse(Delimiter $opener, Delimiter $closer): int
88
    {
89 3
        return $this->findProcessor($opener->getNumDelims())->getDelimiterUse($opener, $closer);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 3
    public function process(Text $opener, Text $closer, int $delimiterUse)
96
    {
97 3
        return $this->findProcessor($delimiterUse)->process($opener, $closer, $delimiterUse);
98
    }
99
100 3
    private function findProcessor(int $len): DelimiterProcessorInterface
101
    {
102
        // Find the "longest" processor which can handle this length
103 3
        foreach ($this->processors as $processor) {
104 3
            if ($processor->getMinLength() <= $len) {
105 3
                return $processor;
106
            }
107
        }
108
109
        // Just use the first one in our list
110
        /** @var DelimiterProcessorInterface $first */
111
        $first = reset($this->processors);
112
113
        return $first;
114
    }
115
}
116