1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace League\HTMLToMarkdown\Converter; |
||
6 | |||
7 | use League\HTMLToMarkdown\Configuration; |
||
8 | use League\HTMLToMarkdown\ConfigurationAwareInterface; |
||
9 | use League\HTMLToMarkdown\ElementInterface; |
||
10 | |||
11 | class CommentConverter implements ConverterInterface, ConfigurationAwareInterface |
||
12 | { |
||
13 | /** @var Configuration */ |
||
14 | protected $config; |
||
15 | |||
16 | public function setConfig(Configuration $config): void |
||
17 | { |
||
18 | $this->config = $config; |
||
19 | 99 | } |
|
20 | |||
21 | 99 | public function convert(ElementInterface $element): string |
|
22 | 99 | { |
|
23 | if ($this->shouldPreserve($element)) { |
||
24 | return '<!--' . $element->getValue() . '-->'; |
||
25 | } |
||
26 | |||
27 | return ''; |
||
28 | } |
||
29 | 6 | ||
30 | /** |
||
31 | 6 | * @return string[] |
|
32 | 3 | */ |
|
33 | public function getSupportedTags(): array |
||
34 | 6 | { |
|
35 | return ['#comment']; |
||
36 | } |
||
37 | |||
38 | private function shouldPreserve(ElementInterface $element): bool |
||
39 | { |
||
40 | 99 | $preserve = $this->config->getOption('preserve_comments'); |
|
41 | if ($preserve === true) { |
||
42 | 99 | return true; |
|
43 | } |
||
44 | |||
45 | if (\is_array($preserve)) { |
||
46 | $value = \trim($element->getValue()); |
||
47 | |||
48 | return \in_array($value, $preserve, true); |
||
49 | } |
||
50 | 6 | ||
51 | return false; |
||
52 | 6 | } |
|
53 | } |
||
54 |