Passed
Push — main ( 989696...2152d5 )
by Colin
05:14 queued 02:09
created

TableCellRenderer::getXmlAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This is part of the league/commonmark package.
7
 *
8
 * (c) Martin Hasoň <[email protected]>
9
 * (c) Webuni s.r.o. <[email protected]>
10
 * (c) Colin O'Dell <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
namespace League\CommonMark\Extension\Table;
17
18
use League\CommonMark\Node\Node;
19
use League\CommonMark\Renderer\ChildNodeRendererInterface;
20
use League\CommonMark\Renderer\NodeRendererInterface;
21
use League\CommonMark\Util\HtmlElement;
22
use League\CommonMark\Xml\XmlNodeRendererInterface;
23
24
final class TableCellRenderer implements NodeRendererInterface, XmlNodeRendererInterface
25
{
26
    /**
27
     * @param TableCell $node
28
     *
29
     * {@inheritDoc}
30
     *
31
     * @psalm-suppress MoreSpecificImplementedParamType
32
     */
33 39
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
34
    {
35 39
        TableCell::assertInstanceOf($node);
36
37 36
        $attrs = $node->data->get('attributes');
38
39 36
        if ($node->getAlign() !== null) {
40 6
            $attrs['align'] = $node->getAlign();
41
        }
42
43 36
        $tag = $node->getType() === TableCell::TYPE_HEADER ? 'th' : 'td';
44
45 36
        return new HtmlElement($tag, $attrs, $childRenderer->renderNodes($node->children()));
46
    }
47
48 3
    public function getXmlTagName(Node $node): string
49
    {
50 3
        return 'table_cell';
51
    }
52
53
    /**
54
     * @param TableCell $node
55
     *
56
     * @return array<string, scalar>
57
     *
58
     * @psalm-suppress MoreSpecificImplementedParamType
59
     */
60 3
    public function getXmlAttributes(Node $node): array
61
    {
62 3
        TableCell::assertInstanceOf($node);
63
64 3
        $ret = ['type' => $node->getType()];
65
66 3
        if (($align = $node->getAlign()) !== null) {
67 3
            $ret['align'] = $align;
68
        }
69
70 3
        return $ret;
71
    }
72
}
73