| Total Complexity | 5 |
| Total Lines | 40 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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() |
|
| 54 | } |
||
| 55 | } |
||
| 56 |