Passed
Push — table-align-css ( 15a600...868262 )
by Colin
10:24 queued 08:10
created

TableCellRenderer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 63
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getXmlTagName() 0 3 1
A getXmlAttributes() 0 11 2
A render() 0 12 3
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\Extension\Attributes\Util\AttributesHelper;
19
use League\CommonMark\Node\Node;
20
use League\CommonMark\Renderer\ChildNodeRendererInterface;
21
use League\CommonMark\Renderer\NodeRendererInterface;
22
use League\CommonMark\Util\HtmlElement;
23
use League\CommonMark\Xml\XmlNodeRendererInterface;
24
25
final class TableCellRenderer implements NodeRendererInterface, XmlNodeRendererInterface
26
{
27
    private const DEFAULT_ATTRIBUTES = [
28
        TableCell::ALIGN_LEFT   => ['align' => 'left'],
29
        TableCell::ALIGN_CENTER => ['align' => 'center'],
30
        TableCell::ALIGN_RIGHT  => ['align' => 'right'],
31
    ];
32
33
    /** @var array<TableCell::ALIGN_*, array<string, string|string[]|bool>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<TableCell::ALIGN_*... string|string[]|bool>> at position 2 could not be parsed: Expected '>' at position 2, but found 'TableCell'.
Loading history...
34
    private array $alignmentAttributes;
35
36
    /**
37
     * @param array<TableCell::ALIGN_*, array<string, string|string[]|bool>> $alignmentAttributes
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<TableCell::ALIGN_*... string|string[]|bool>> at position 2 could not be parsed: Expected '>' at position 2, but found 'TableCell'.
Loading history...
38
     */
39 140
    public function __construct(array $alignmentAttributes = self::DEFAULT_ATTRIBUTES)
40
    {
41 140
        $this->alignmentAttributes = $alignmentAttributes;
42
    }
43
44
    /**
45
     * @param TableCell $node
46
     *
47
     * {@inheritDoc}
48
     *
49
     * @psalm-suppress MoreSpecificImplementedParamType
50
     */
51 64
    public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
52
    {
53 64
        TableCell::assertInstanceOf($node);
54
55 62
        $attrs = $node->data->get('attributes');
56 62
        if (($alignment = $node->getAlign()) !== null) {
57 14
            $attrs = AttributesHelper::mergeAttributes($attrs, $this->alignmentAttributes[$alignment]);
58
        }
59
60 62
        $tag = $node->getType() === TableCell::TYPE_HEADER ? 'th' : 'td';
61
62 62
        return new HtmlElement($tag, $attrs, $childRenderer->renderNodes($node->children()));
63
    }
64
65 2
    public function getXmlTagName(Node $node): string
66
    {
67 2
        return 'table_cell';
68
    }
69
70
    /**
71
     * @param TableCell $node
72
     *
73
     * @return array<string, scalar>
74
     *
75
     * @psalm-suppress MoreSpecificImplementedParamType
76
     */
77 2
    public function getXmlAttributes(Node $node): array
78
    {
79 2
        TableCell::assertInstanceOf($node);
80
81 2
        $ret = ['type' => $node->getType()];
82
83 2
        if (($align = $node->getAlign()) !== null) {
84 2
            $ret['align'] = $align;
85
        }
86
87 2
        return $ret;
88
    }
89
}
90