TableToForm   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 79
dl 0
loc 145
rs 9.84
c 7
b 0
f 1
wmc 32

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldWithLabel() 0 8 2
A fields() 0 22 5
A field() 0 20 5
A label() 0 11 2
A compute_field_value() 0 10 3
A computeFieldValue() 0 13 3
C fieldByType() 0 47 12
1
<?php
2
3
namespace HexMakina\kadro;
4
5
use HexMakina\BlackBox\ORM\ModelInterface;
6
use HexMakina\Marker\{Form,Element};
7
8
class TableToForm
9
{
10
    private static function compute_field_value($model, $field_name)
11
    {
12
        $ret = '';
13
        if (method_exists($model, $field_name)) {
14
            $ret = $model->$field_name();
15
        }elseif (property_exists($model, $field_name)) {
16
            $ret = $model->$field_name;
17
        }
18
19
        return $ret;
20
    }
21
22
    public static function label($model, $field_name, $attributes = [], $errors = [])
23
    {
24
        $label_content = $field_name;
25
26
        if (isset($attributes['label'])) {
27
            $label_content = $attributes['label'];
28
            unset($attributes['label']);
29
        }
30
31
        $ret = Form::label($field_name, $label_content, $attributes, $errors);
32
        return $ret;
33
    }
34
35
    public static function field(ModelInterface $model, $field_name, $attributes = [], $errors = []): string
36
    {
37
        $field_value = $attributes['value'] ?? self::computeFieldValue($model, $field_name);
38
        $attributes['name'] = $attributes['name'] ?? $field_name;
39
40
        $table = get_class($model)::table();
41
42
        if (is_null($table->column($field_name))) {
43
            return Form::input($field_name, $field_value, $attributes, $errors);
44
        }
45
46
        $field = $table->column($field_name);
47
48
        if (isset($attributes['required']) && $attributes['required'] === false) {
49
            unset($attributes['required']);
50
        } elseif (!$field->isNullable()) {
51
            $attributes[] = 'required';
52
        }
53
54
        return self::fieldByType($field, $field_value, $attributes, $errors);
55
    }
56
57
    public static function fieldWithLabel($model, $field_name, $attributes = [], $errors = []): string
58
    {
59
        $field_attributes = $attributes;
60
        if (isset($attributes['label'])) {
61
            unset($field_attributes['label']);
62
        }
63
64
        return sprintf('%s %s', self::label($model, $field_name, $attributes, $errors), self::field($model, $field_name, $field_attributes, $errors));
65
    }
66
67
    public static function fields(ModelInterface $model, $group_class = null): string
68
    {
69
        $table = get_class($model)::table();
70
        $ret = '';
71
        foreach ($table->columns() as $column_name => $column) {
72
          // vd($column_name);
73
74
            $form_field = '';
75
            if ($column->isAutoIncremented()) {
76
                if (!$model->isNew()) {
77
                    $form_field = self::field($model, $column_name);
78
                }
79
            } else {
80
                $form_field = self::fieldWithLabel($model, $column_name);
81
                if (!is_null($group_class)) {
82
                    $form_field = new Element('div', $form_field, ['class' => $group_class]);
83
                }
84
            }
85
            $ret .= PHP_EOL . $form_field;
86
        }
87
88
        return $ret;
89
    }
90
91
    private static function fieldByType($field, $field_value, $attributes = [], $errors = []): string
92
    {
93
        $ret = null;
94
        if ($field->isAutoIncremented()) {
95
            $ret = Form::hidden($field->name(), $field_value);
96
        }
97
        elseif ($field->type()->isBoolean()) {
98
            $option_list = $attributes['values'] ?? [0 => 0, 1 => 1];
99
            $ret = Form::select($field->name(), $option_list, $field_value, $attributes); //
100
        }
101
        elseif ($field->type()->isInteger()) {
102
            $ret = Form::input($field->name(), $field_value, $attributes, $errors);
103
        }
104
        elseif ($field->type()->isYear()) {
105
            $attributes['size'] = $attributes['maxlength'] = 4;
106
            $ret = Form::input($field->name(), $field_value, $attributes, $errors);
107
        }
108
        elseif ($field->type()->isDate()) {
109
            $ret = Form::date($field->name(), $field_value, $attributes, $errors);
110
        }
111
        elseif ($field->type()->isTime()) {
112
            $ret = Form::time($field->name(), $field_value, $attributes, $errors);
113
        }
114
        elseif ($field->type()->isDatetime()) {
115
            $ret = Form::datetime($field->name(), $field_value, $attributes, $errors);
116
        }
117
        elseif ($field->type()->isText()) {
118
            $ret = Form::textarea($field->name(), $field_value, $attributes, $errors);
119
        }
120
        elseif ($field->type()->isEnum()) {
121
            $enum_values = [];
122
            foreach ($field->type()->getEnumValues() as $e_val) {
123
                $enum_values[$e_val] = $e_val;
124
            }
125
126
            $selected = $attributes['value'] ?? $field_value ?? '';
127
            $ret = Form::select($field->name(), $enum_values, $selected, $attributes); //
128
        }
129
        elseif ($field->type()->isString()) {
130
            $max_length = $field->type()->getLength();
131
            $attributes['size'] = $attributes['maxlength'] = $max_length;
132
            $ret = Form::input($field->name(), $field_value, $attributes, $errors);
133
        }
134
        else
135
          $ret = Form::input($field->name(), $field_value, $attributes, $errors);
136
137
        return $ret;
138
    }
139
140
    private static function computeFieldValue($model, $field_name)
141
    {
142
        $ret = '';
143
        
144
        if (method_exists($model, $field_name)) {
145
            $ret = $model->$field_name();
146
        }
147
148
        if (property_exists($model, $field_name)) {
149
            $ret = $model->$field_name;
150
        }
151
152
        return $ret;
153
    }
154
}
155