Passed
Push — master ( 9047ed...66f7ef )
by Rougin
01:30
created

ListItemConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 44
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 26 4
A getSupportedTags() 0 3 1
1
<?php
2
3
namespace Pilipinews\Common\Converters;
4
5
use League\HTMLToMarkdown\Converter\ConverterInterface;
6
use League\HTMLToMarkdown\ElementInterface;
7
8
/**
9
 * List Item Converter
10
 *
11
 * @package Pilipinews
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 */
14
class ListItemConverter implements ConverterInterface
15
{
16
    /**
17
     * Converts the specified element into a parsed string.
18
     *
19
     * @param  \League\HTMLToMarkdown\ElementInterface $element
20
     * @return string
21
     */
22 3
    public function convert(ElementInterface $element)
23
    {
24
        // If parent is an ol, use numbers, otherwise, use dashes
25 3
        $list_type = $element->getParent()->getTagName();
26
27
        // Add spaces to start for nested list items
28 3
        $level = $element->getListItemLevel($element);
29
30 3
        $prefixForParagraph = str_repeat('  ', $level + 1);
31
32 3
        $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($element->getValue()))));
33
34
        // If list item is the first in a nested list, add a newline before it
35 3
        $prefix = '';
36
37 3
        if ($level > 0 && $element->getSiblingPosition() === 1) {
38
            $prefix = "\n";
39
        }
40
41 3
        if ($list_type === 'ol') {
42 3
            $number = (string) $element->getSiblingPosition();
43
44 3
            return $prefix . $number . '. ' . $value . "\n";
45
        }
46
47 3
        return $prefix . '* ' . $value . "\n";
48
    }
49
50
    /**
51
     * Returns the supported HTML tags.
52
     *
53
     * @return string[]
54
     */
55 12
    public function getSupportedTags()
56
    {
57 12
        return array('li');
58
    }
59
}
60