ListItemConverter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 40
ccs 12
cts 12
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupportedTags() 0 3 1
A convert() 0 22 4
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 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
        $tagname = $element->getParent()->getTagName();
26
27
        // Add spaces to start for nested list items
28 3
        $level = $element->getListItemLevel($element);
29
30 3
        $items = explode("\n", trim($element->getValue()));
31
32 3
        $paragraphPrefix = str_repeat('  ', $level + 1);
33
34 3
        $value = implode("\n" . $paragraphPrefix, $items);
35
36 3
        $number = (integer) $element->getSiblingPosition();
37
38
        // If list item is the first in a nested list, add a newline before it
39 3
        $prefix = $level > 0 && $number === 1 ? "\n" : '';
40
41 3
        $position = $tagname === 'ol' ? $number . '. ' : '* ';
42
43 3
        return $prefix . $position . trim($value) . "\n";
44
    }
45
46
    /**
47
     * Returns the supported HTML tags.
48
     *
49
     * @return string[]
50
     */
51 15
    public function getSupportedTags()
52
    {
53 15
        return array('li');
54
    }
55
}
56