Passed
Push — master ( edc575...08ade3 )
by Bruno
07:00
created

Table::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 15
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 20
rs 9.7666
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\HTML\Element;
4
5
use Formularium\Element;
6
use Formularium\HTMLNode;
7
use Formularium\Metadata;
8
use Formularium\MetadataParameter;
9
10
class Table extends Element
11
{
12
    const ROW_NAMES = 'rownames';
13
    const ROW_DATA = 'rowdata';
14
    const STRIPED = 'striped';
15
    const BORDERED = 'bordered';
16
17
    public function render(array $parameters, HTMLNode $previous): HTMLNode
18
    {
19
        $headrows = [];
20
        $footrows = [];
21
        if (array_key_exists(self::ROW_NAMES, $parameters)) {
22
            foreach ($parameters[self::ROW_NAMES] as $rowname) {
23
                $headrows[] = HTMLNode::factory(
24
                    'th',
25
                    [
26
                        'class' => 'formularium-table__th'
27
                    ],
28
                    $rowname
29
                );
30
                $footrows[] = HTMLNode::factory(
31
                    'th',
32
                    [
33
                        'class' => 'formularium-table__th'
34
                    ],
35
                    $rowname
36
                );
37
            }
38
        }
39
40
        $rowdata = [];
41
        if (array_key_exists(self::ROW_DATA, $parameters)) {
42
            foreach ($parameters[self::ROW_DATA] as $data) {
43
                $rowdata[] = HTMLNode::factory(
44
                    'tr',
45
                    [
46
                        'class' => 'formularium-table__td'
47
                    ],
48
                    array_map(
49
                        function ($i) {
50
                            return HTMLNode::factory('td', [], $i);
51
                        },
52
                        $data
53
                    )
54
                );
55
            }
56
        }
57
58
        return HTMLNode::factory(
59
            'table',
60
            [
61
                'class' => 'formularium-table'
62
            ],
63
            [
64
                HTMLNode::factory(
65
                    'thead',
66
                    ['class' => 'formularium-table__head'],
67
                    HTMLNode::factory(
68
                        'tr',
69
                        ['class' => 'formularium-table__headrow'],
70
                        $headrows
71
                    ),
72
                ),
73
                HTMLNode::factory(
74
                    'tfoot',
75
                    ['class' => 'formularium-table__foot'],
76
                    HTMLNode::factory(
77
                        'tr',
78
                        ['class' => 'formularium-table__footrow'],
79
                        $footrows
80
                    ),
81
                ),
82
                HTMLNode::factory(
83
                    'tbody',
84
                    ['class' => 'formularium-table__body'],
85
                    $rowdata
86
                )
87
            ]
88
        );
89
    }
90
91
    public static function getMetadata(): Metadata
92
    {
93
        return new Metadata(
94
            'Table',
95
            'Creates a table',
96
            [
97
                new MetadataParameter(
98
                    static::ROW_NAMES,
99
                    'array',
100
                    'Is it disabled?'
101
                ),
102
                new MetadataParameter(
103
                    static::STRIPED,
104
                    'bool',
105
                    'Is this table striped?'
106
                ),
107
                new MetadataParameter(
108
                    static::BORDERED,
109
                    'bool',
110
                    'Is this table bordered?'
111
                )
112
            ]
113
        );
114
    }
115
}
116