Completed
Push — 1.5 ( fb52dd...2fb592 )
by Colin
02:48
created

AttributesInlineParser::parse()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.8817
c 0
b 0
f 0
cc 6
nc 7
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) 2015 Martin Hasoň <[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
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Attributes\Parser;
16
17
use League\CommonMark\Extension\Attributes\Node\AttributesInline;
18
use League\CommonMark\Inline\Parser\InlineParserInterface;
19
use League\CommonMark\InlineParserContext;
20
21
final class AttributesInlineParser implements InlineParserInterface
22
{
23
    use AttributesParserTrait;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 18
    public function getCharacters(): array
29
    {
30 18
        return [' ', '{'];
31
    }
32
33 18
    public function parse(InlineParserContext $inlineContext): bool
34
    {
35 18
        $cursor = $inlineContext->getCursor();
36 18
        if ($cursor->getNextNonSpaceCharacter() !== '{') {
37 15
            return false;
38
        }
39
40 18
        $char = $cursor->getCharacter();
41 18
        if ($char === '{') {
42 18
            $char = (string) $cursor->getCharacter($cursor->getPosition() - 1);
43
        }
44
45 18
        $attributes = $this->parseAttributes($cursor);
46 18
        if ($attributes === []) {
47 3
            return false;
48
        }
49
50 18
        if ($char === '') {
51 12
            $cursor->advanceToNextNonSpaceOrNewline();
52
        }
53
54 18
        $node = new AttributesInline($attributes, $char === ' ' || $char === '');
55 18
        $inlineContext->getContainer()->appendChild($node);
56
57 18
        return true;
58
    }
59
}
60