Completed
Push — master ( 9bba17...b2b071 )
by Colin
09:14 queued 05:11
created

ListItemConverter::convert()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.7972
cc 4
eloc 13
nc 4
nop 1
crap 4
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\ElementInterface;
6
7
class ListItemConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 6
    public function convert(ElementInterface $element)
15
    {
16
        // If parent is an ol, use numbers, otherwise, use dashes
17 6
        $list_type = $element->getParent()->getTagName();
18 6
        $value = $element->getValue();
19
20
        // Add spaces to start for nested list items
21 6
        $level = $element->getListItemLevel($element);
22 6
        $prefix = str_repeat('  ', $level);
23
        // If list item is the first in a nested list, add a newline before it
24 6
        if ($level > 0 && $element->getSiblingPosition() === 1) {
25 3
            $prefix = "\n" . $prefix;
26 3
        }
27
28 6
        if ($list_type === 'ul') {
29 6
            $markdown = $prefix . '- ' . trim($value) . "\n";
30 6
        } else {
31 6
            $number = $element->getSiblingPosition();
32 6
            $markdown = $prefix . $number . '. ' . trim($value) . "\n";
33
        }
34
35 6
        return $markdown;
36
    }
37
38
    /**
39
     * @return string[]
40
     */
41 69
    public function getSupportedTags()
42
    {
43 69
        return array('li');
44
    }
45
}
46