Completed
Pull Request — master (#139)
by Philip
04:58
created

ListItemConverter::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\Configuration;
6
use League\HTMLToMarkdown\ConfigurationAwareInterface;
7
use League\HTMLToMarkdown\ElementInterface;
8
9
class ListItemConverter implements ConverterInterface, ConfigurationAwareInterface
10
{
11
    /**
12
     * @var Configuration
13
     */
14
    protected $config;
15
16
    /**
17
     * @param Configuration $config
18
     */
19 81
    public function setConfig(Configuration $config)
20
    {
21 81
        $this->config = $config;
22 81
    }
23
24
    /**
25
     * @param ElementInterface $element
26
     *
27
     * @return string
28
     */
29 6
    public function convert(ElementInterface $element)
30
    {
31
        // If parent is an ol, use numbers, otherwise, use dashes
32 6
        $list_type = $element->getParent()->getTagName();
33
34
        // Add spaces to start for nested list items
35 6
        $level = $element->getListItemLevel($element);
36
37 6
        $prefixForParagraph = str_repeat('  ', $level + 1);
38 6
        $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($element->getValue()))));
39
40
        // If list item is the first in a nested list, add a newline before it
41 6
        $prefix = '';
42 6
        if ($level > 0 && $element->getSiblingPosition() === 1) {
43 3
            $prefix = "\n";
44 3
        }
45
46 6
        if ($list_type === 'ul') {
47 6
            $list_item_style = $this->config->getOption('list_item_style', '-');
48 6
            return $prefix . $list_item_style . ' ' . $value . "\n";
49
        }
50
51 6
        $number = $element->getSiblingPosition();
52
53 6
        return $prefix . $number . '. ' . $value . "\n";
54
    }
55
56
    /**
57
     * @return string[]
58
     */
59 81
    public function getSupportedTags()
60
    {
61 81
        return array('li');
62
    }
63
}
64