Passed
Push — master ( bb4653...2ab0f7 )
by Bruno
07:17
created

Button   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
dl 0
loc 52
rs 10
c 1
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadata() 0 20 1
A render() 0 18 3
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 Button extends Element
11
{
12
    const COLOR = 'color';
13
    const COLOR_PRIMARY = 'primary';
14
    const COLOR_LINK = 'link';
15
    const COLOR_INFO = 'info';
16
    const COLOR_SUCCESS = 'success';
17
    const COLOR_WARNING = 'warning';
18
    const COLOR_ERROR = 'error';
19
    
20
    const TYPE = 'type';
21
22
    public function render(array $parameters, HTMLNode $previous): HTMLNode
23
    {
24
        $node = new HTMLNode('button');
25
26
        $node->setAttributes([
27
            'type' => $parameters[self::TYPE] ?? 'button',
28
            'class' => '',
29
        ]);
30
31
        $node->setContent($parameters[self::LABEL] ?? '');
32
33
        foreach ([static::DISABLED, static::READONLY] as $v) {
34
            if ($parameters[$v] ?? false) {
35
                $node->setAttribute($v, $v);
36
            }
37
        }
38
39
        return $node;
40
    }
41
42
    public static function getMetadata(): Metadata
43
    {
44
        return new Metadata(
45
            'Button',
46
            'Creates a button',
47
            [
48
                new MetadataParameter(
49
                    static::TYPE,
50
                    'string',
51
                    'Button type, like "submit", "reset" or "button" . Default: "button"'
52
                ),
53
                new MetadataParameter(
54
                    static::READONLY,
55
                    'boolean',
56
                    'Is it readonly?'
57
                ),
58
                new MetadataParameter(
59
                    static::DISABLED,
60
                    'boolean',
61
                    'Is it disabled?'
62
                )
63
            ]
64
        );
65
    }
66
}
67