Completed
Pull Request — master (#154)
by
unknown
03:37
created

ListItemConverter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 102
ccs 25
cts 40
cp 0.625
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setConfig() 0 24 5
D convert() 0 33 10
A getSupportedTags() 0 4 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
   * @var bool
18
   */
19
    protected $doAlternatePrefixes = FALSE;
20
21
  /**
22
   * @var array
23
   */
24
    protected $prefixes;
25
26
  /**
27
   * @var array
28
   */
29
    protected $standardPrefixes;
30
31
  /**
32
   * @var array
33
   */
34
    protected $alternatePrefixes;
35
36
    /**
37
     * @param Configuration $config
38
     */
39 84
    public function setConfig(Configuration $config)
40
    {
41 84
        $this->config = $config;
42 84
        $list_item_alternate = $this->config->getOption('list_item_alternate');
43 84
        if (isset($list_item_alternate)) {
44
            foreach (['standard', 'alternate'] as $item) {
45
                if (!isset($list_item_alternate[$item])) {
46
                   throw new \Exception("The '{$item}' property is missing in 'list_item_alternate' config of HtmlConverter.");
47
                }
48
                if (count($list_item_alternate[$item]) != 2) {
49
                   throw new \Exception("The '{$item}' property (in 'list_item_alternate' config of HtmlConverter) should have 2 entries.");
50
                }
51
            }
52
53
            $this->doAlternatePrefixes = TRUE;
54
            $this->prefixes = $list_item_alternate['alternate'];
55
            $this->standardPrefixes = $list_item_alternate['standard'];
56
            $this->alternatePrefixes = $list_item_alternate['alternate'];
57
        }
58
        else {
59 84
            $list_item_style = $this->config->getOption('list_item_style', '-');
60 84
            $this->prefixes = [$list_item_style, '.'];
61
        }
62 84
    }
63
64
    /**
65
     * @param ElementInterface $element
66
     *
67
     * @return string
68
     */
69 6
    public function convert(ElementInterface $element)
70
    {
71
        // If parent is an ol, use numbers, otherwise, use dashes
72 6
        $list_type = $element->getParent()->getTagName();
73
74
        // Add spaces to start for nested list items
75 6
        $level = $element->getListItemLevel($element);
76
77 6
        $prefixForParagraph = str_repeat('  ', $level + 1);
78 6
        $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($element->getValue()))));
79
80
        // If list item is the first in a nested list, add a newline before it
81 6
        $prefix = '';
82 6
        if ($level > 0 && $element->getSiblingPosition() === 1) {
83 3
            $prefix = "\n";
84 2
        }
85 6
        elseif ($level == 0 && $element->getSiblingPosition() === 1 && $this->doAlternatePrefixes) {
86
          $this->prefixes = ($this->prefixes == $this->standardPrefixes) ? $this->alternatePrefixes : $this->standardPrefixes;
87
        }
88
89 6
        if ($list_type === 'ul') {
90 6
            $list_item_style = $this->prefixes[0];
91 6
            return $prefix . $list_item_style . ' ' . $value . "\n";
92
        }
93
94 3
        if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) {
95
            $number = $start + $element->getSiblingPosition() - 1;
96
        } else {
97 3
            $number = $element->getSiblingPosition();
98
        }
99
100 3
        return $prefix . $number . $this->prefixes[1] . ' ' . $value . "\n";
101
    }
102
103
    /**
104
     * @return string[]
105
     */
106 84
    public function getSupportedTags()
107
    {
108 84
        return array('li');
109
    }
110
}
111