Passed
Push — main ( 02be03...8407ab )
by Sammy
02:05
created

Form::elementWithErrors()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 4
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\Marker;
4
5
/**
6
*
7
*/
8
class Form
9
{
10
11
    public static function __callStatic($element_type, $arguments)
12
    {
13
      // arguments [name, value, [attributes], [errors]]
14
        $i = 0;
15
        $field_name =  $arguments[$i++] ?? null;
16
        $field_value = $arguments[$i++] ?? null;
17
        $attributes =  $arguments[$i++] ?? [];
18
        $errors =      $arguments[$i++] ?? [];
19
20
21
        $attributes['type'] = $element_type;
22
        $attributes['name'] = $attributes['name'] ?? $field_name;
23
        $attributes['value'] = $attributes['value'] ?? $field_value;
24
25
        switch ($attributes['type']) {
26
            case 'datetime':
27
                $attributes['type'] = 'datetime-local';
28
                break;
29
30
            case 'password':
31
                $attributes['value'] = '';
32
                break;
33
        }
34
        return self::input($field_name, $field_value, $attributes, $errors);
35
    }
36
37
    public static function input($field_name, $field_value = null, $attributes = [], $errors = [])
38
    {
39
40
        if (isset($attributes['disabled']) || !isset($attributes['type']) || in_array('disabled', $attributes, true)) {
41
            $attributes['type'] = 'text';
42
        } else {
43
            $attributes['type'] = $attributes['type'] ?? 'text';
44
        }
45
46
        $attributes['name'] = $attributes['name'] ?? $field_name;
47
        $attributes['value'] = $attributes['value'] ?? $field_value;
48
49
        return self::elementWithErrors('input', null, $attributes, isset($errors[$field_name]));
50
    }
51
52
    public static function textarea($field_name, $field_value = null, $attributes = [], $errors = [])
53
    {
54
        $attributes['name'] = $attributes['name'] ?? $field_name;
55
        return self::elementWithErrors('textarea', $field_value, $attributes, isset($errors[$field_name]));
56
    }
57
58
    public static function select($field_name, $option_list, $selected = null, $attributes = [], $errors = [])
59
    {
60
        $attributes['name'] = $attributes['name'] ?? $field_name;
61
62
        $options = '';
63
        foreach ($option_list as $value => $label) {
64
            $option_attributes = ['value' => $value];
65
            if ($selected == $value) {
66
                $option_attributes['selected'] =  'selected';
67
            }
68
69
            $options .= new Element('option', $label, $option_attributes);
70
        }
71
        return self::elementWithErrors('select', $options, $attributes, isset($errors[$field_name]));
72
    }
73
74
    public static function legend($label, $attributes = [])
75
    {
76
        return new Element('legend', $label, $attributes);
77
    }
78
79
    public static function label($field_for, $field_label, $attributes = [], $errors = [])
80
    {
81
        $attributes['for'] = $field_for;
82
        return self::elementWithErrors('label', $field_label, $attributes, isset($errors[$field_for]));
83
    }
84
85
    public static function submit($field_id, $field_label, $attributes = [])
86
    {
87
        $attributes['id'] = $attributes['id'] ?? $field_id;
88
        unset($attributes['name']);
89
        $attributes['type'] = 'submit';
90
        if (isset($attributes['tag']) && $attributes['tag'] === 'input') {
91
            return new Element('input', '', $attributes);
92
        } else {
93
            unset($attributes['tag']);
94
            unset($attributes['value']);
95
            return new Element('button', $field_label, $attributes);
96
        }
97
    }
98
99
    private static function elementWithErrors($tag, $content, $attributes = [], $errors = false)
100
    {
101
102
        $attributes['id'] = $attributes['id'] ?? $attributes['name'] ?? '';
103
104
        if ($errors === true) {
105
            $attributes['class'] = $attributes['class'] ?? '';
106
            $attributes['class'] .= ' error';
107
        }
108
109
        $label = '';
110
        if (isset($attributes['label'])) {
111
            $label = self::label($attributes['id'], $attributes['label'], [], $errors);
112
            unset($attributes['label']);
113
        }
114
115
      // vd($attributes, $tag);
116
        return $label . (new Element($tag, $content, $attributes));
117
    }
118
}
119