Passed
Push — master ( 74ddc4...4602de )
by Mohammad
03:46
created

formGenerator::generateInputs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: shanmaseen
5
 * Date: 09/04/19
6
 * Time: 02:44 م
7
 */
8
9
namespace Shamaseen\Repository\Generator\Forms;
10
11
12
use DB;
13
use Doctrine\DBAL\Schema\Column;
14
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class formGenerator
17
{
18
    private $inputs;
19
20
    /**
21
     * @param Column $column
22
     * @return string
23
     */
24
    function generateFormInput($column) {
25
        $fileInput = $this->getFormInputClass($column);
26
        return $fileInput->template();
27
    }
28
29
    /**
30
     * @param Column $column
31
     * @return Input|TextArea
32
     */
33
    function getFormInputClass($column)
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...
34
    {
35
        switch ($column->getType()->getName())
36
        {
37
            case "integer":
38
            case "int":
39
            case "mediumint":
40
            case "bigint":
41
            case "decimal":
42
            case "float":
43
            case "double":
44
                return new Input($column);
45
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
            case "enum":
47
                return new Input($column);
48
                break;
49
            case "date":
50
            case "datetime":
51
            case "timestamp":
52
            case "time":
53
            case "year":
54
                return new Input($column);
55
                break;
56
            case "text":
57
                return new TextArea($column);
58
                break;
59
            case "boolean":
60
            case "bool":
61
                return new Input($column);
62
                break;
63
            case "varchat":
64
            default:
65
                return new Input($column);
66
        }
67
    }
68
69
    /**
70
     * @param Model $entity
71
     * @param string $method
72
     * @return string
73
     */
74
    function generateForm($entity,$method = 'post')
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...
75
    {
76
        $html = '<form method="post" action="#">
77
           <input type="hidden" name="__method" value="'.$method.'">';
78
        $html .= $this->getInputs($entity);
79
        $html .= '</form>';
80
        return $html;
81
    }
82
83
    /**
84
     * @param Model $entity
85
     * @return array
86
     */
87
    function getFillables($entity)
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...
88
    {
89
        if(!empty($entity->getFillable()))
90
            return $entity->getFillable();
91
92
        $columns = \Schema::getColumnListing($entity->getTable());
93
94
        foreach ($entity->getGuarded() as $guarded)
95
        {
96
            if (($key = array_search($guarded, $columns)) !== false) {
97
                unset($columns[$key]);
98
            }
99
        }
100
101
        return $columns;
102
    }
103
104
    /**
105
     * @param Model $entity
106
     * @return string
107
     */
108
    function getInputs($entity)
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...
109
    {
110
        if($this->inputs)
111
            return $this->inputs;
112
113
        return $this->generateInputs($entity);
114
    }
115
116
    /**
117
     * @param Model $entity
118
     * @return string
119
     */
120
    function generateInputs($entity)
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...
121
    {
122
        $html = '';
123
        foreach ($this->getFillables($entity) as $fillable)
124
        {
125
            $column = DB::connection()->getDoctrineColumn($entity->getTable(),$fillable);
126
127
            $html .= $this->generateFormInput($column);
128
        }
129
        $html .= '<button type="submit" class="btn btn-primary">Submit</button>';
130
        $this->inputs = $html;
131
132
        return $html;
133
    }
134
}