Passed
Push — master ( b99ed2...868915 )
by Reza
03:24
created

StubParser::parseRelationName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace EasyPanel\Commands\Actions;
5
6
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
use Symfony\Component\Console\Exception\CommandNotFoundException;
10
11
trait StubParser
12
{
13
14
    public function getDefaultNamespace($rootNamespace)
15
    {
16
        $name = ucfirst($this->getNameInput());
0 ignored issues
show
Bug introduced by
It seems like getNameInput() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

16
        $name = ucfirst($this->/** @scrutinizer ignore-call */ getNameInput());
Loading history...
17
        $this->path = parent::getDefaultNamespace($rootNamespace)."\\Http\\Livewire\\Admin\\$name";
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
19
        return $this->path;
20
    }
21
22
    public function replaceModel($stub)
23
    {
24
        $fields = $this->getConfig('fields');
25
26
        $modelNamespace = $this->parseModel($this->getConfig('model'));
27
        $modelName = $this->getModelName($modelNamespace);
28
29
        $array = [
30
            '{{ modelName }}' => $modelName,
31
            '{{ modelNamespace }}' => $modelNamespace,
32
            '{{ model }}' => strtolower($modelName),
33
            '{{ properties }}' => $this->parseProperties($fields),
34
            '{{ rules }}' => $this->parseValidationRules(),
35
            '{{ fields }}' => $this->parseFields($fields),
36
            '{{ setProperties }}' => $this->parsePropertiesValue($fields),
37
        ];
38
39
        return str_replace(array_keys($array), array_values($array), $stub);
40
    }
41
42
    public function parseBlade($stub){
43
        $modelNamespace = $this->parseModel($this->getConfig('model'));
44
        $modelName = $this->getModelName($modelNamespace);
45
        $array = [
46
            '{{ model }}' => strtolower($modelName),
47
            '{{ modelName }}' => $modelName,
48
            '{{ data }}' => $this->parseDataInBlade($modelName),
49
            '{{ titles }}' => $this->parseTitlesInBlade(),
50
            '{{ inputs }}' => $this->parseInputsInBlade(),
51
        ];
52
53
        return str_replace(array_keys($array), array_values($array), $stub);
54
    }
55
56
    public function parseModel($model)
57
    {
58
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
59
            throw new InvalidArgumentException('Model name contains invalid characters.');
60
        }
61
62
        return $this->qualifyModel($model);
0 ignored issues
show
Bug introduced by
It seems like qualifyModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
        return $this->/** @scrutinizer ignore-call */ qualifyModel($model);
Loading history...
63
    }
64
65
    public function getConfig($key, $action = null){
66
        $action = $action ?? $this->getNameInput();
67
68
        if(config('easy_panel.crud.'.$action.'.'.$key)){
69
            return config('easy_panel.crud.'.$action.'.'.$key);
70
        }
71
72
        return [];
73
    }
74
75
    public function getModelName($modelNamespace)
76
    {
77
        $array = explode('\\', $modelNamespace);
78
79
        return end($array);
80
    }
81
82
    public function parseProperties($fields)
83
    {
84
        $fields = array_keys($fields);
85
        $str = '';
86
        foreach ($fields as $field) {
87
            $str .= 'public $'.$field.";".$this->makeTab(1);
88
        }
89
90
        return $str;
91
    }
92
93
    public function parsePropertiesValue($fields)
94
    {
95
        $fields = array_keys($fields);
96
        $str = '';
97
        $action = $this->getNameInput();
98
        foreach ($fields as $field) {
99
            $str .= '$this->'.$field.' = $this->'.$action.'->'.$field.';'.$this->makeTab(2, end($fields) != $field);
100
        }
101
102
        return $str;
103
    }
104
105
    public function parseValidationRules()
106
    {
107
        $rules = $this->getConfig('validation');
108
109
        $str = '';
110
        foreach ($rules as $key => $rule) {
111
            $str .= "'$key' => '$rule',".$this->makeTab(2, $rule != end($rules));
112
        }
113
114
        return $str;
115
    }
116
117
    public function parseFields($fields)
118
    {
119
        $str = '';
120
        foreach ($fields as $key => $field) {
121
            $str .= $field != end($fields) ? "'$key' => " . '$this' . "->$key,".$this->makeTab(3) : "'$key' => " . '$this' . "->$key,";
122
        }
123
124
        $model = $this->getNameInput();
125
        if(config("easy_panel.crud.$model.extra_values")){
126
            $str .= $this->parseExtraValues();
127
        }
128
129
        return $str;
130
    }
131
132
    public function parseExtraValues(){
133
        $str = '';
134
135
        $values = $this->getConfig('extra_values');
136
        foreach ($values as $key => $value) {
137
            $str .= $this->makeTab(3, end($values) != $values)."'$key' => $value,";
138
        }
139
140
        return $str;
141
    }
142
143
    public function parseDataInBlade($modelName)
144
    {
145
        $fields = $this->getConfig('show');
146
        $str = '';
147
        $modelName = strtolower($modelName);
148
        foreach ($fields as $value) {
149
            if (!is_array($value)) {
150
                $str .= '<td> {{ $' . $modelName . '->' . $value . " }} </td>" . $this->makeTab(1, end($fields) != $value);
151
            } else {
152
                $relationName = $this->parseRelationName(array_key_first($value));
153
                $str .= '<td> {{ $' . $modelName . '->' . $relationName . '->'. $value[array_key_first($value)] .' }} </td>' . $this->makeTab(1, end($fields) != $value);
154
            }
155
        }
156
157
        return $str;
158
    }
159
160
    public function parseTitlesInBlade()
161
    {
162
        $fields = $this->getConfig('show');
163
        $str = '';
164
        foreach ($fields as $field) {
165
            if (!is_array($field)) {
166
                $field = ucfirst($field);
167
            } else {
168
                $field = ucfirst($field[array_key_first($field)]);
169
            }
170
            $str .= "<td> $field </td>".$this->makeTab(6, end($fields) != $field);
171
        }
172
173
        return $str;
174
    }
175
176
    public function parseInputsInBlade()
177
    {
178
        $fields = $this->getConfig('fields');
179
180
        $str = '';
181
        foreach ($fields as $key => $type) {
182
            $str .= '<div class="form-group">'.$this->makeTab(4);
183
            $str .= '<label for="input'.$key.'" class="col-sm-2 control-label">'.ucfirst($key).'</label>'.$this->makeTab(4);
184
            $str = $this->inputsHTML($type, $key, $str).$this->makeTab(4);
185
            $str .='@error("'.$key.'") <div class="invalid-feedback">{{ $message }}</div> @enderror'.$this->makeTab(3);
186
            $str .= '</div>'.$this->makeTab(3);
187
        }
188
189
        return $str;
190
    }
191
192
    public function inputsHTML($type, $key, string $str): string
193
    {
194
        $array = [
195
            'text' => '<input type="text" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
196
            'email' => '<input type="email" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
197
            'number' => '<input type="number" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
198
            'file' => '<input type="file" wire:model="' . $key . '" class="form-control-file @error(\''.$key.'\')is-invalid @enderror" id="input' . $key . '">',
199
            'textarea' => '<textarea wire:model="' . $key . '" class="form-control @error(\''.$key.'\')is-invalid @enderror"></textarea>',
200
            'password' => '<input type="password" wire:model.lazy="' . $key . '" class="form-control  @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
201
        ];
202
        $str .= $array[$type];
203
204
        return $str;
205
    }
206
207
    public function makeTab($count, $newLine = true){
208
        $count = $count * 4;
209
        $tabs = str_repeat(' ', $count);
210
211
        return $newLine ? "\n".$tabs : $tabs;
212
    }
213
214
    public function parseRelationName($column){
215
        $name = explode('_', $column);
216
217
        return $name[0];
218
    }
219
220
}
221