Completed
Push — master ( d9ee7a...0393a1 )
by Iman
14s
created

FormConfigGenerator::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator;
4
5
use crocodicstudio\crudbooster\helpers\DbInspector;
6
use Illuminate\Support\Facades\Schema;
7
use CB;
8
9
class FormConfigGenerator
10
{
11
    /**
12
     * @param $table
13
     * @param $coloms
14
     * @return array
15
     */
16
    static function generateFormConfig($table, $coloms)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    {
18
        $formArrayString = [];
19
        foreach ($coloms as $i => $colName) {
20
            //$attribute = [];
21
            $placeholder = '';
22
            $help = '';
23
24
            $label = self::getLabel($colName);
25
26
            if (FieldDetector::isExceptional($colName)) {
27
                continue;
28
            }
29
30
            $typeData = DbInspector::getFieldTypes($table, $colName);
31
32
            $validation = [];
33
            $validation[] = 'required';
34
            list($type, $rule, $options) = self::parseFieldType($typeData);
35
            $validation[] = $rule;
36
37
            if (FieldDetector::isForeignKey($colName)) {
38
                list($type, $options) = self::handleForeignKey($colName);
39
            }
40
41
            if (substr($colName, 0, 3) == 'is_') {
42
                $type = 'radio_dataenum';
43
                $label = ucwords(substr($colName, 3));
44
                $validation = ['required|integer'];
45
                $options = [
46
                    'enum' => ['In '.$label, $label],
47
                    'value' => [0, 1],
48
                ];
49
            }
50
51
            if (FieldDetector::isPassword($colName)) {
52
                $type = 'password';
53
                $validation = ['min:3', 'max:32'];
54
                $help = cbTrans("text_default_help_password");
55
            }
56
57
            if (FieldDetector::isImage($colName)) {
58
                $type = 'upload';
59
                $validation = ['required|image'];
60
                $help = cbTrans('text_default_help_upload');
61
            }
62
63
            if (FieldDetector::isGeographical($colName)) {
64
                $type = 'hidden';
65
            }
66
67
            if (FieldDetector::isPhone($colName)) {
68
                $type = 'number';
69
                $validation = ['required', 'numeric'];
70
                $placeholder = cbTrans('text_default_help_number');
71
            }
72
73
            if (FieldDetector::isEmail($colName)) {
74
                $type = 'email';
75
                $validation[] = 'email|unique:'.$table;
76
                $placeholder = cbTrans('text_default_help_email');
77
            }
78
79
            if ($type == 'text' && FieldDetector::isNameField($colName)) {
80
                $validation = ['required', 'string', 'min:3', 'max:70'];
81
                $placeholder = cbTrans('text_default_help_text');
82
            }
83
84
            if ($type == 'text' && FieldDetector::isUrlField($colName)) {
85
                $validation = ['required', 'url'];
86
                $placeholder = cbTrans('text_default_help_url');
87
            }
88
89
            $validation = implode('|', $validation);
90
91
            $formArray = [];
92
            $formArray['label'] = $label;
93
            $formArray['name'] = $colName;
94
            $formArray['type'] = $type;
95
            $formArray['options'] = $options;
96
            $formArray['required'] = true;
97
            $formArray['validation'] = $validation;
98
            $formArray['help'] = $help;
99
            $formArray['placeholder'] = $placeholder;
100
            $formArrayString[] = min_var_export($formArray, "            ");
101
        }
102
103
        return $formArrayString;
104
    }
105
106
    /**
107
     * @param $c
108
     * @return mixed|string
109
     */
110
    private static function getLabel($c)
111
    {
112
        $label = str_replace("id_", "", $c);
113
        $label = ucwords(str_replace("_", " ", $label));
114
115
        return $label;
116
    }
117
118
    /**
119
     * @param $typeData
120
     * @param $validation
121
     * @return array
122
     */
123
    private static function parseFieldType($typeData)
124
    {
125
        $typeData = array_get([
126
            'longtext' => 'text',
127
            'integer' => 'int',
128
            'timestamp' => 'datetime',
129
        ], $typeData, $typeData);
130
131
132
        $default = ["text", "min:1|max:255", []];
133
        return array_get([
134
            'text' => ['textarea', "string|min:5", []],
135
            'date' => ['date', "date", ['php_format' => 'M, d Y', 'datepicker_format' => 'M, dd YYYY',]],
136
            'datetime' => ['datetime', "date_format:Y-m-d H:i:s", ['php_format' => 'M, d Y H:i',]],
137
            'time' => ['time', 'date_format:H:i:s', []],
138
            'double' => ['money', "integer|min:0", []],
139
            'int' => ['number', 'integer|min:0', []],
140
        ], $typeData, $default);
0 ignored issues
show
Bug introduced by
It seems like $typeData can also be of type array<string,string>; however, parameter $key of array_get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
        ], /** @scrutinizer ignore-type */ $typeData, $default);
Loading history...
141
    }
142
143
    /**
144
     * @param $field
145
     * @return array
146
     */
147
    private static function handleForeignKey($field)
148
    {
149
        $jointable = str_replace(['id_', '_id'], '', $field);
150
        if (Schema::hasTable($jointable)) {
151
            $joincols = CB::getTableColumns($jointable);
152
            $joinname = CB::getNameTable($joincols);
153
            $jointablePK = CB::pk($jointable);
154
            $type = 'select2_datatable';
155
            $options = [
156
                'table' => $jointable,
157
                'field_label' => $joinname,
158
                'field_value' => $jointablePK,
159
            ];
160
        }
161
162
        return [$type, $options];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
163
    }
164
}