Completed
Pull Request — master (#8)
by
unknown
08:47
created

AttributesInlineParser::parse()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 16
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 7
nop 1
crap 42
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-attributes-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\AttributesExtension;
14
15
use League\CommonMark\Delimiter\Delimiter;
16
use League\CommonMark\Inline\Parser\AbstractInlineParser;
17
use League\CommonMark\InlineParserContext;
18
19
class AttributesInlineParser extends AbstractInlineParser
20
{
21 5
    public function getCharacters()
22
    {
23 5
        return [' ', '{'];
24
    }
25
26
    public function parse(InlineParserContext $inlineContext)
27
    {
28
        $cursor = $inlineContext->getCursor();
29
        if ($cursor->getNextNonSpaceCharacter() !== '{') {
30
            return false;
31
        }
32
33
        $char = $cursor->getCharacter();
34
        if ('{' === $char) {
35
            $char = (string) $cursor->getCharacter($cursor->getPosition() - 1);
36
        }
37
38
        $attributes = AttributesUtils::parse($cursor);
39
        if (empty($attributes)) {
40
            return false;
41
        }
42
43
        if ('' === $char) {
44
            $cursor->advanceToNextNonSpaceOrNewline();
45
        }
46
47
        $node = new InlineAttributes($attributes, ' ' === $char || '' === $char);
48
        $inlineContext->getContainer()->appendChild($node);
49
        $inlineContext->getDelimiterStack()->push(new Delimiter('attributes', 1, $node, false, false));
50
51
        return true;
52
    }
53
}
54