Completed
Push — master ( e25879...032337 )
by Colin
01:39
created

ListItemConverter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 73
ccs 29
cts 29
cp 1
rs 10

3 Methods

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