FormGenerator   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 116
rs 10
wmc 28

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generateFormInput() 0 5 1
A generateForm() 0 8 1
A getFillables() 0 15 4
D getFormInputClass() 0 23 18
A getInputs() 0 7 2
A generateInputs() 0 12 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mohammad Shamaseen
5
 * Date: 09/04/19
6
 * Time: 02:44 م.
7
 */
8
9
namespace Shamaseen\Repository\Generator\Forms;
10
11
use DB;
12
use Doctrine\DBAL\Schema\Column;
13
use Illuminate\Database\Eloquent\Model;
14
use Schema;
15
16
class FormGenerator
17
{
18
    private $inputs;
19
20
    /**
21
     * @param Column $column
22
     *
23
     * @return string
24
     */
25
    public function generateFormInput(Column $column): string
26
    {
27
        $fileInput = $this->getFormInputClass($column);
28
29
        return $fileInput->template();
30
    }
31
32
    /**
33
     * @param Column $column
34
     *
35
     * @return Input|TextArea
36
     */
37
    public function getFormInputClass(Column $column)
38
    {
39
        switch ($column->getType()->getName()) {
40
            case 'text':
41
                return new TextArea($column);
42
            case 'integer':
43
            case 'int':
44
            case 'mediumint':
45
            case 'bigint':
46
            case 'decimal':
47
            case 'float':
48
            case 'double':
49
            case 'enum':
50
            case 'date':
51
            case 'datetime':
52
            case 'timestamp':
53
            case 'time':
54
            case 'bool':
55
            case 'year':
56
            case 'boolean':
57
            case 'varchat':
58
            default:
59
                return new Input($column);
60
        }
61
    }
62
63
    /**
64
     * @param Model  $entity
65
     * @param string $method
66
     *
67
     * @return string
68
     */
69
    public function generateForm(Model $entity, string $method = 'post'): string
70
    {
71
        $html = '<form method="post" action="#">
72
           <input type="hidden" name="__method" value="' . $method . '">';
73
        $html .= $this->getInputs($entity);
74
        $html .= '</form>';
75
76
        return $html;
77
    }
78
79
    /**
80
     * @param Model $entity
81
     *
82
     * @return array
83
     */
84
    public function getFillables(Model $entity): array
85
    {
86
        if (!empty($entity->getFillable())) {
87
            return $entity->getFillable();
88
        }
89
90
        $columns = Schema::getColumnListing($entity->getTable());
91
92
        foreach ($entity->getGuarded() as $guarded) {
93
            if (false !== ($key = array_search($guarded, $columns))) {
94
                unset($columns[$key]);
95
            }
96
        }
97
98
        return $columns;
99
    }
100
101
    /**
102
     * @param Model $entity
103
     *
104
     * @return string
105
     */
106
    public function getInputs(Model $entity): string
107
    {
108
        if ($this->inputs) {
109
            return $this->inputs;
110
        }
111
112
        return $this->generateInputs($entity);
113
    }
114
115
    /**
116
     * @param Model $entity
117
     *
118
     * @return string
119
     */
120
    public function generateInputs(Model $entity): string
121
    {
122
        $html = '';
123
        foreach ($this->getFillables($entity) as $fillable) {
124
            $column = DB::connection()->getDoctrineColumn($entity->getTable(), $fillable);
125
126
            $html .= $this->generateFormInput($column);
127
        }
128
        $html .= "<button type='submit' class='btn btn-primary'>Submit</button>";
129
        $this->inputs = $html;
130
131
        return $html;
132
    }
133
}
134