Passed
Push — master ( baf3f0...bfaf94 )
by Reza
04:09
created

StubParser::parseExtraValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
4
namespace EasyPanel\Commands\Actions;
5
6
7
use InvalidArgumentException;
8
use Symfony\Component\Console\Exception\CommandNotFoundException;
9
10
trait StubParser
11
{
12
13
    protected function getDefaultNamespace($rootNamespace)
14
    {
15
        $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

15
        $name = ucfirst($this->/** @scrutinizer ignore-call */ getNameInput());
Loading history...
16
        $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...
17
18
        return $this->path;
19
    }
20
21
    protected function replaceModel($stub)
22
    {
23
        $fields = $this->getConfig('fields');
24
        if(!$fields) {
25
            throw new CommandNotFoundException("There is no `field` in your config file");
26
        }
27
28
        $modelNamespace = $this->parseModel($this->getConfig('model'));
29
        $modelName = $this->getModelName($modelNamespace);
30
31
        $array = [
32
            '{{ modelName }}' => $modelName,
33
            '{{ modelNamespace }}' => $modelNamespace,
34
            '{{ model }}' => strtolower($modelName),
35
            '{{ properties }}' => $this->parseProperties($fields),
36
            '{{ rules }}' => $this->parseValidationRules(),
37
            '{{ fields }}' => $this->parseFields($fields),
38
            '{{ setProperties }}' => $this->parsePropertiesValue($fields),
39
        ];
40
41
        return str_replace(array_keys($array), array_values($array), $stub);
42
    }
43
44
    protected function parseBlade($stub){
45
        $modelNamespace = $this->parseModel($this->getConfig('model'));
46
        $modelName = $this->getModelName($modelNamespace);
47
        $array = [
48
            '{{ model }}' => strtolower($modelName),
49
            '{{ modelName }}' => $modelName,
50
            '{{ data }}' => $this->parseDataInBlade($modelName),
51
            '{{ titles }}' => $this->parseTitlesInBlade(),
52
            '{{ inputs }}' => $this->parseInputsInBlade(),
53
        ];
54
55
        return str_replace(array_keys($array), array_values($array), $stub);
56
    }
57
58
    protected function parseModel($model)
59
    {
60
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
61
            throw new InvalidArgumentException('Model name contains invalid characters.');
62
        }
63
64
        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

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