Passed
Push — master ( 284e72...816a06 )
by Iman
04:12
created

FormConfigGenerator::generateFormConfig()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 8
nop 2
dl 0
loc 46
rs 8.4751
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 crocodicstudio\crudbooster\Modules\ModuleGenerator\FileManipulator;
7
use Illuminate\Support\Facades\Schema;
8
9
class FormConfigGenerator
10
{
11
    /**
12
     * @param $table
13
     * @param $columns
14
     * @return array
15
     */
16
    public static function generateFormConfig($table, $columns)
17
    {
18
        $formArrayString = [];
19
        foreach ($columns as $i => $colName) {
20
            //$attribute = [];
21
            $input = [
22
                'label' => self::getLabel($colName),
23
                'name' => $colName,
24
                'type' => '',
25
                'options' => '',
26
                'required' => true,
27
                'validation' => '',
28
                'help' => '',
29
                'placeholder' => '',
30
            ];
31
32
            if (FieldDetector::isExceptional($colName)) {
33
                continue;
34
            }
35
36
            $typeData = \Schema::getColumnType($table, $colName);
37
38
            $input = array_merge($input, self::parseFieldType($typeData));
39
40
            if (FieldDetector::isForeignKey($colName)) {
41
                list($input['type'], $input['options']) = self::handleForeignKey($colName);
42
            } elseif (substr($colName, 0, 3) == 'is_') {
43
                $input['type'] = 'radio_dataenum';
44
                $label = ucwords(substr($colName, 3));
45
                $input['options'] = [
46
                    'enum' => ['In '.$label, $label],
47
                    'value' => [0, 1],
48
                ];
49
            }
50
51
            $props = array_get(DefaultFormConfigs::defaultConfigForFields($table), FieldDetector::detect($colName), null);
52
53
            if($props !== null){
54
                $input = array_merge($input, $props);
55
            }
56
57
            $indent = str_repeat(' ', 8);
58
            $formArrayString[] = FileManipulator::stringify($input, $indent);
59
        }
60
61
        return $formArrayString;
62
    }
63
64
    /**
65
     * @param $c
66
     * @return mixed|string
67
     */
68
    private static function getLabel($c)
69
    {
70
        $label = str_replace("id_", "", $c);
71
        $label = ucwords(str_replace("_", " ", $label));
72
73
        return $label;
74
    }
75
76
    /**
77
     * @param $typeData
78
     *
79
     * @return array
80
     */
81
    private static function parseFieldType($typeData)
82
    {
83
        // if matched a key, overrides $typeData to corresponding values
84
        $typeData = array_get([
85
            'longtext' => 'text',
86
            'integer' => 'int',
87
            'timestamp' => 'datetime',
88
        ], $typeData, $typeData);
89
90
        $default = ["text", "min:1|max:255", []];
91
92
        $arr = array_get([
93
            'string' => $default,
94
            'text' => ['textarea', "string|min:5", []],
95
            'date' => ['date', "date", ['php_format' => 'M, d Y', 'datepicker_format' => 'M, dd YYYY',]],
96
            'datetime' => ['datetime', "date_format:Y-m-d H:i:s", ['php_format' => 'M, d Y H:i',]],
97
            'time' => ['time', 'date_format:H:i:s', []],
98
            'double' => ['money', "integer|min:0", []],
99
            'int' => ['number', 'integer|min:0', []],
100
        ], $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

100
        ], /** @scrutinizer ignore-type */ $typeData, $default);
Loading history...
101
102
        return [
103
            'type' => $arr[0],
104
            'validation' => $arr[1],
105
            'options' => $arr[2],
106
        ];
107
    }
108
109
    /**
110
     * @param $field
111
     * @return array
112
     */
113
    private static function handleForeignKey($field)
114
    {
115
        $jointable = str_replace(['id_', '_id'], '', $field);
116
        if (! Schema::hasTable($jointable)) {
117
            return ['', ''];
118
        }
119
        $options = [
120
            'table' => $jointable,
121
            'field_label' => DbInspector::colName(\Schema::getColumnListing($jointable)),
122
            'field_value' => DbInspector::findPk($jointable),
123
        ];
124
125
        return ['select2_datatable', $options];
126
    }
127
}