Completed
Pull Request — master (#179)
by
unknown
25:02
created

CommentConverter::shouldPreserve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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