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

ListItemConverter::convert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 26
ccs 11
cts 13
cp 0.8462
crap 4.0582
rs 9.9
c 0
b 0
f 0
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