BootstrapForm   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 26
c 0
b 0
f 0
dl 0
loc 80
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A input() 0 13 4
A select() 0 14 3
A submit() 0 3 1
A getValue() 0 6 3
A surround() 0 3 1
1
<?php
2
3
namespace Core\Services\HTML;
4
5
6
/**
7
 * Generate a Bootstap form quickly and easily
8
 */
9
class BootstrapForm
10
{
11
    /**
12
     * @var array Data used by the form
13
     */
14
    protected $data;
15
16
    /**
17
     * @param array $data Data used by the form
18
     * @return void
19
     */
20
    public function __construct($data = [])
21
    {
22
        $this->data = $data;
23
    }
24
25
    /**
26
     * @param string $index Index of the value to be recovered
27
     * @return string
28
     */
29
    protected function getValue(string $index)
30
    {
31
        if (is_object($this->data)) {
0 ignored issues
show
introduced by
The condition is_object($this->data) is always false.
Loading history...
32
            return $this->data->$index;
33
        }
34
        return isset($this->data[$index]) ? $this->data[$index] : \null;
35
    }
36
37
    /**
38
     * @param string $html Code HTML to surround it
39
     * @return string
40
     */
41
    protected function surround(string $html)
42
    {
43
        return "<div class='form-floating'>{$html}</div>";
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param string $label
49
     * @param array $option
50
     * @return string
51
     */
52
    public function input(string $name, string $label, array $option = [])
53
    {
54
        $type = !empty($option["type"]) ? $option["type"] : "text";
55
        $maxlength = !empty($option["maxlength"]) ? "maxlength='{$option['maxlength']}'" : \null;
56
        if ($type === "textarea") {
57
            $input = "<textarea class='form-control' name='{$name}' {$maxlength} style='height: 200px' placeholder='' required>{$this->getValue($name)}</textarea>";
58
            $label = "<label for='{$name}'>{$label}</label>";
59
        } else {
60
            $input = "<input type='{$type}' class='form-control' name='{$name}' value='{$this->getValue($name)}' {$maxlength} placeholder='' required>";
61
            $label = "<label for='{$name}' class='floatingInput'>{$label}</label>";
62
        }
63
64
        return $this->surround($input . $label);
65
    }
66
67
    public function select($name, $label, $options)
68
    {
69
        $label = "<label for='floatingSelect'>{$label}</label>";
70
        $input = "<select class='form-select' id='floatingSelect' name='{$name}'>";
71
        foreach ($options as $k => $v) {
72
            $attributes = "";
73
            if ($k == $this->getValue($name)) {
74
                $attributes = 'selected';
75
            }
76
            $input .= "<option value='{$k}' {$attributes}>{$v}</option>";
77
        }
78
        $input .= "</select>";
79
80
        return $this->surround($input . $label);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function submit()
87
    {
88
        return $this->surround("<button type='submit' class='btn btn-primary'>Envoyer</button>");
89
    }
90
}
91