Completed
Push — master ( cfa612...41c7dc )
by Colin
9s
created

EmphasisConverter::convert()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.8571
cc 6
eloc 12
nc 9
nop 1
crap 6
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 EmphasisConverter implements ConverterInterface, ConfigurationAwareInterface
10
{
11
    /**
12
     * @var Configuration
13
     */
14
    protected $config;
15
16
    /**
17
     * @param Configuration $config
18
     */
19 84
    public function setConfig(Configuration $config)
20
    {
21 84
        $this->config = $config;
22 84
    }
23
24
    /**
25
     * @param ElementInterface $element
26
     *
27
     * @return string
28
     */
29 12
    public function convert(ElementInterface $element)
30
    {
31 12
        $tag = $element->getTagName();
32 12
        $value = $element->getValue();
33
34 12
        if (!trim($value)) {
35 3
            return $value;
36
        }
37
38 12
        if ($tag === 'i' || $tag === 'em') {
39 9
            $style = $this->config->getOption('italic_style');
40 6
        } else {
41 9
            $style = $this->config->getOption('bold_style');
42
        }
43
44 12
        $prefix = ltrim($value) !== $value ? ' ' : '';
45 12
        $suffix = rtrim($value) !== $value ? ' ' : '';
46
47 12
        return $prefix . $style . trim($value) . $style . $suffix;
48
    }
49
50
    /**
51
     * @return string[]
52
     */
53 84
    public function getSupportedTags()
54
    {
55 84
        return array('em', 'i', 'strong', 'b');
56
    }
57
}
58