Completed
Pull Request — master (#159)
by
unknown
02:47
created

ListItemConverter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 76
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 4 1
B convert() 0 32 6
A getSupportedTags() 0 4 1
A escapeSpecialCharacters() 0 8 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 84
    public function setConfig(Configuration $config)
20
    {
21 84
        $this->config = $config;
22 84
    }
23
24
    /**
25
     * @param ElementInterface $element
26
     *
27
     * @return string
28
     */
29 9
    public function convert(ElementInterface $element)
30
    {
31
        // If parent is an ol, use numbers, otherwise, use dashes
32 9
        $list_type = $element->getParent()->getTagName();
33
34 9
        $value = $this->escapeSpecialCharacters($element->getValue());
35
36
        // Add spaces to start for nested list items
37 9
        $level = $element->getListItemLevel($element);
38
39 9
        $prefixForParagraph = str_repeat('  ', $level + 1);
40 9
        $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($value))));
41
42
        // If list item is the first in a nested list, add a newline before it
43 9
        $prefix = '';
44 9
        if ($level > 0 && $element->getSiblingPosition() === 1) {
45 3
            $prefix = "\n";
46 2
        }
47
48 9
        if ($list_type === 'ul') {
49 9
            $list_item_style = $this->config->getOption('list_item_style', '-');
50 9
            return $prefix . $list_item_style . ' ' . $value . "\n";
51
        }
52
53 3
        if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) {
54 3
            $number = $start + $element->getSiblingPosition() - 1;
55 2
        } else {
56 3
            $number = $element->getSiblingPosition();
57
        }
58
59 3
        return $prefix . $number . '. ' . $value . "\n";
60
    }
61
62
    /**
63
     * @return string[]
64
     */
65 84
    public function getSupportedTags()
66
    {
67 84
        return array('li');
68
    }
69
70
    /**
71
     * @param string $line
72
     *
73
     * @return string
74
     */
75 9
    private function escapeSpecialCharacters($line)
76
    {
77 9
        return preg_replace(
78 9
            "/(.*)\n([ ]*)(-|\*|\+) (.*)/",
79 9
            "$1\n$2\\\\$3 $4",
80 3
            $line
81 6
        );
82
    }
83
84
}
85