Completed
Push — master ( 63adb9...1faad8 )
by Colin
02:08
created

CommentConverter::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 CommentConverter implements ConverterInterface, ConfigurationAwareInterface
10
{
11
    /**
12
     * @var Configuration
13
     */
14
    protected $config;
15
16
    /**
17
     * @param Configuration $config
18
     */
19 90
    public function setConfig(Configuration $config)
20
    {
21 90
        $this->config = $config;
22 90
    }
23
24
    /**
25
     * @param ElementInterface $element
26
     *
27
     * @return string
28
     */
29 6
    public function convert(ElementInterface $element)
30
    {
31 6
        if ($this->shouldPreserve($element)) {
32 3
            return '<!--' . $element->getValue() . '-->';
33
        }
34 6
        return '';
35
    }
36
37
    /**
38
     * @return string[]
39
     */
40 90
    public function getSupportedTags()
41
    {
42 90
        return array('#comment');
43
    }
44
45
    /**
46
     * @param ElementInterface $element
47
     *
48
     * @return bool
49
     */
50 6
    private function shouldPreserve(ElementInterface $element)
51
    {
52 6
        $preserve = $this->config->getOption('preserve_comments');
53 6
        if ($preserve === true) {
54 3
            return true;
55
        }
56 6
        if (is_array($preserve)) {
57 3
            $value = trim($element->getValue());
58 3
            return in_array($value, $preserve);
59
        }
60 3
        return false;
61
    }
62
}
63