TableConverter   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 96.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 100
ccs 53
cts 55
cp 0.9636
rs 10
wmc 24

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 3 1
C convert() 0 52 16
A getSupportedTags() 0 3 1
A preConvert() 0 12 6
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
use League\HTMLToMarkdown\PreConverterInterface;
11
12
class TableConverter implements ConverterInterface, PreConverterInterface, ConfigurationAwareInterface
13
{
14
    /** @var Configuration */
15
    protected $config;
16
17
    public function setConfig(Configuration $config): void
18
    {
19 3
        $this->config = $config;
20
    }
21 3
22 3
    /** @var array<string, string> */
23
    private static $alignments = [
24
        'left' => ':--',
25
        'right' => '--:',
26
        'center' => ':-:',
27
    ];
28
29
    /** @var array<int, string>|null */
30
    private $columnAlignments = [];
31
32
    /** @var string|null */
33
    private $caption = null;
34
35
    public function preConvert(ElementInterface $element): void
36
    {
37
        $tag = $element->getTagName();
38
        // Only table cells and caption are allowed to contain content.
39
        // Remove all text between other table elements.
40
        if ($tag === 'th' || $tag === 'td' || $tag === 'caption') {
41
            return;
42
        }
43
44
        foreach ($element->getChildren() as $child) {
45
            if ($child->isText()) {
46
                $child->setFinalMarkdown('');
47
            }
48 3
        }
49
    }
50 3
51
    public function convert(ElementInterface $element): string
52
    {
53 3
        $value = $element->getValue();
54 3
55 3
        switch ($element->getTagName()) {
56 3
            case 'table':
57 2
                $this->columnAlignments = [];
58 2
                if ($this->caption) {
59 2
                    $side = $this->config->getOption('table_caption_side');
60 3
                    if ($side === 'top') {
61
                        $value = $this->caption . "\n" . $value;
62
                    } elseif ($side === 'bottom') {
63
                        $value .= $this->caption;
64
                    }
65
66
                    $this->caption = null;
67 3
                }
68
69 3
                return $value . "\n";
70
            case 'caption':
71 3
                $this->caption = \trim($value);
72 3
73 3
                return '';
74 3
            case 'tr':
75 3
                $value .= "|\n";
76 3
                if ($this->columnAlignments !== null) {
77 3
                    $value .= '|' . \implode('|', $this->columnAlignments) . "|\n";
78 3
79 3
                    $this->columnAlignments = null;
80 2
                }
81 3
82 2
                return $value;
83 3
            case 'th':
84 3
            case 'td':
85 3
                if ($this->columnAlignments !== null) {
86 3
                    $align = $element->getAttribute('align');
87 3
88 3
                    $this->columnAlignments[] = self::$alignments[$align] ?? '---';
89 3
                }
90 3
91 3
                $value = \str_replace("\n", ' ', $value);
92 2
                $value = \str_replace('|', $this->config->getOption('table_pipe_escape') ?? '\|', $value);
93 3
94 3
                return '| ' . \trim($value) . ' ';
95 3
            case 'thead':
96 3
            case 'tbody':
97 3
            case 'tfoot':
98 3
            case 'colgroup':
99 2
            case 'col':
100 3
                return $value;
101 3
            default:
102 3
                return '';
103 3
        }
104 3
    }
105 3
106 3
    /**
107 3
     * @return string[]
108 3
     */
109
    public function getSupportedTags(): array
110
    {
111
        return ['table', 'tr', 'th', 'td', 'thead', 'tbody', 'tfoot', 'colgroup', 'col', 'caption'];
112
    }
113
}
114