Passed
Pull Request — master (#1112)
by Iman
04:03
created

FormConfigGenerator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
C generateFormConfig() 0 68 14
A handleForeignKey() 0 13 2
A parseFieldType() 0 18 1
A getLabel() 0 6 1
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator;
4
5
use crocodicstudio\crudbooster\helpers\DbInspector;
6
use crocodicstudio\crudbooster\Modules\ModuleGenerator\FileManipulator;
7
use Illuminate\Support\Facades\Schema;
8
use CB;
9
10
class FormConfigGenerator
11
{
12
    /**
13
     * @param $table
14
     * @param $coloms
15
     * @return array
16
     */
17
    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...
18
    {
19
        $formArrayString = [];
20
        foreach ($coloms as $i => $colName) {
21
            //$attribute = [];
22
            $input = [
23
                'label' => self::getLabel($colName),
24
                'name' => $colName,
25
                'type' => '',
26
                'options' => '',
27
                'required' => true,
28
                'validation' => '',
29
                'help' => '',
30
                'placeholder' => '',
31
            ];
32
33
            if (FieldDetector::isExceptional($colName)) {
34
                continue;
35
            }
36
37
            $typeData = DbInspector::getFieldTypes($table, $colName);
38
39
            list($input['type'], $input['validation'], $input['options']) = self::parseFieldType($typeData);
40
41
            if (FieldDetector::isForeignKey($colName)) {
42
                list($input['type'], $input['options']) = self::handleForeignKey($colName);
43
            }
44
45
            if (substr($colName, 0, 3) == 'is_') {
46
                $input['type'] = 'radio_dataenum';
47
                $label = ucwords(substr($colName, 3));
48
                $input['options'] = [
49
                    'enum' => ['In '.$label, $label],
50
                    'value' => [0, 1],
51
                ];
52
            }
53
54
            if (FieldDetector::isPassword($colName)) {
55
                $input['type'] = 'password';
56
                $input['validation'] = 'min:5|max:32|required';
57
                $input['help'] = cbTrans("text_default_help_password");
58
            }elseif (FieldDetector::isImage($colName)) {
59
                $input['type'] = 'upload';
60
                $input['validation'] = 'required|image';
61
                $input['help'] = cbTrans('text_default_help_upload');
62
            }elseif (FieldDetector::isGeographical($colName)) {
63
                $input['type'] = 'hidden';
64
                $input['validation'] = 'required|numeric';
65
            }elseif (FieldDetector::isPhone($colName)) {
66
                $input['type'] = 'number';
67
                $input['validation'] = 'required|numeric';
68
                $input['placeholder'] = cbTrans('text_default_help_number');
69
            }elseif (FieldDetector::isEmail($colName)) {
70
                $input['type'] = 'email';
71
                $input['validation'] = 'require|email|unique:'.$table;
72
                $input['placeholder'] = cbTrans('text_default_help_email');
73
            }elseif ($input['type'] == 'text' && FieldDetector::isNameField($colName)) {
74
                $input['validation'] = 'required|string|min:3|max:70';
75
                $input['placeholder'] = cbTrans('text_default_help_text');
76
            }elseif ($input['type'] == 'text' && FieldDetector::isUrlField($colName)) {
77
                $input['validation'] = 'required|url';
78
                $input['placeholder'] = cbTrans('text_default_help_url');
79
            }
80
81
            $formArrayString[] = FileManipulator::stringify($input, "            ");
82
        }
83
84
        return $formArrayString;
85
    }
86
87
    /**
88
     * @param $c
89
     * @return mixed|string
90
     */
91
    private static function getLabel($c)
92
    {
93
        $label = str_replace("id_", "", $c);
94
        $label = ucwords(str_replace("_", " ", $label));
95
96
        return $label;
97
    }
98
99
    /**
100
     * @param $typeData
101
     *
102
     * @return array
103
     */
104
    private static function parseFieldType($typeData)
105
    {
106
        $typeData = array_get([
107
            'longtext' => 'text',
108
            'integer' => 'int',
109
            'timestamp' => 'datetime',
110
        ], $typeData, $typeData);
111
112
113
        $default = ["text", "min:1|max:255", []];
114
        return array_get([
115
            'text' => ['textarea', "string|min:5", []],
116
            'date' => ['date', "date", ['php_format' => 'M, d Y', 'datepicker_format' => 'M, dd YYYY',]],
117
            'datetime' => ['datetime', "date_format:Y-m-d H:i:s", ['php_format' => 'M, d Y H:i',]],
118
            'time' => ['time', 'date_format:H:i:s', []],
119
            'double' => ['money', "integer|min:0", []],
120
            'int' => ['number', 'integer|min:0', []],
121
        ], $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

121
        ], /** @scrutinizer ignore-type */ $typeData, $default);
Loading history...
122
    }
123
124
    /**
125
     * @param $field
126
     * @return array
127
     */
128
    private static function handleForeignKey($field)
129
    {
130
        $jointable = str_replace(['id_', '_id'], '', $field);
131
        if (!Schema::hasTable($jointable)) {
132
            return ['', ''];
133
        }
134
        $options = [
135
            'table' => $jointable,
136
            'field_label' => DbInspector::colName(DbInspector::getTableCols($jointable)),
137
            'field_value' => DbInspector::findPk($jointable),
138
        ];
139
140
        return ['select2_datatable', $options];
141
    }
142
}