Passed
Push — main ( c1f98d...69b186 )
by Sammy
06:34 queued 05:03
created

Form   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 57
c 3
b 1
f 0
dl 0
loc 113
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 14 3
A legend() 0 3 1
A textarea() 0 4 1
A label() 0 4 1
A __callStatic() 0 24 3
A input() 0 13 4
A elementWithErrors() 0 18 3
A submit() 0 15 3
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['type'] = 'submit';
88
        unset($attributes['name']);
89
90
        $attributes['id'] = $attributes['id'] ?? $field_id;
91
        $attributes['value'] = $attributes['value'] ?? $field_label;
92
93
        if (isset($attributes['tag']) && $attributes['tag'] === 'input') {
94
            unset($attributes['tag']);
95
            return new Element('input', '', $attributes);
96
        } else {
97
            unset($attributes['tag']);
98
            unset($attributes['value']);
99
            return new Element('button', $field_label, $attributes);
100
        }
101
    }
102
103
    private static function elementWithErrors($tag, $content, $attributes = [], $errors = false)
104
    {
105
106
        $attributes['id'] = $attributes['id'] ?? $attributes['name'] ?? '';
107
108
        if ($errors === true) {
109
            $attributes['class'] = $attributes['class'] ?? '';
110
            $attributes['class'] .= ' error';
111
        }
112
113
        $label = '';
114
        if (isset($attributes['label'])) {
115
            $label = self::label($attributes['id'], $attributes['label'], [], $errors);
116
            unset($attributes['label']);
117
        }
118
119
      // vd($attributes, $tag);
120
        return $label . (new Element($tag, $content, $attributes));
121
    }
122
}
123