Passed
Push — master ( 648548...5e4c5e )
by Bruno
08:25
created

Button   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 41
c 1
b 0
f 1
dl 0
loc 61
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 46 5
A getMetadata() 0 11 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Bootstrapvue\Element;
4
5
use Formularium\Element;
6
use Formularium\Exception\Exception;
7
use Formularium\HTMLNode;
8
use Formularium\Frontend\HTML\Element\Button as HTMLButton;
9
use Formularium\Metadata;
10
use Formularium\MetadataParameter;
11
12
class Button extends Element
13
{
14
    public function render(array $parameters, HTMLNode $previous): HTMLNode
15
    {
16
        $previous->setTag('b-button');
17
18
        $color = $parameters[HTMLButton::COLOR] ?? '';
19
        $colorMap = [
20
            HTMLButton::COLOR_PRIMARY => 'primary',
21
            HTMLButton::COLOR_LINK => 'link',
22
            HTMLButton::COLOR_INFO => 'info',
23
            HTMLButton::COLOR_SUCCESS => 'success',
24
            HTMLButton::COLOR_WARNING => 'warning',
25
            HTMLButton::COLOR_ERROR => 'error',
26
        ];
27
28
        if (array_key_exists($color, $colorMap)) {
29
            $color = $parameters[HTMLButton::COLOR];
30
        } else {
31
            $colors = [
32
                'primary',
33
                'success',
34
                'danger',
35
                'warning',
36
                'info',
37
                'link',
38
                'light',
39
                'dark',
40
            ];
41
            if (!in_array($color, $colors)) {
42
                throw new Exception('Invalid button color.');
43
            }
44
        }
45
        $previous->addAttribute('variant', $color);
46
47
        $size = $parameters[self::SIZE] ?? '';
48
        switch ($size) {
49
            case self::SIZE_LARGE:
50
                $previous->addAttribute('size', 'lg');
51
                break;
52
            case self::SIZE_SMALL:
53
                $previous->addAttribute('size', 'sm');
54
                break;
55
        }
56
57
        // TODO $icon = $parameters[self::ICON] ?? '';
58
59
        return $previous;
60
    }
61
62
    public static function getMetadata(): Metadata
63
    {
64
        $metadata = HTMLButton::getMetadata();
65
        $metadata->appendParameter(
66
            new MetadataParameter(
67
                HTMLButton::COLOR,
68
                'string',
69
                'Button color. Supports HTMLButton::COLOR_PRIMARY, HTMLButton::COLOR_LINK, HTMLButton::COLOR_INFO, HTMLButton::COLOR_SUCCESS, HTMLButton::COLOR_WARNING, HTMLButton::COLOR_ERROR. Default: primary.'
70
            )
71
        );
72
        return $metadata;
73
    }
74
}
75