Passed
Push — master ( e8aeaf...845edf )
by Reza
03:16
created

StubParser::makeTab()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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(1);
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
        return $str;
127
    }
128
129
    protected function parseDataInBlade($modelName)
130
    {
131
        $fields = $this->getConfig('show');
132
        $str = '';
133
        $modelName = strtolower($modelName);
134
        foreach ($fields as $value) {
135
            $str .= '<td> {{ $'.$modelName.'->'.$value." }} </td>".$this->makeTab(1);
136
        }
137
138
        return $str;
139
    }
140
141
    protected function parseTitlesInBlade()
142
    {
143
        $fields = $this->getConfig('show');
144
        $str = '';
145
        foreach ($fields as $field) {
146
            $field = ucfirst($field);
147
            $str .= "<td> $field </td>".$this->makeTab(6);
148
        }
149
150
        return $str;
151
    }
152
153
    protected function parseInputsInBlade()
154
    {
155
        $fields = $this->getConfig('fields');
156
157
        $str = '';
158
        foreach ($fields as $key => $type) {
159
            $str .= '<div class="form-group"><label for="input'.$key.'" class="col-sm-2 control-label">'.ucfirst($key).'</label>'.PHP_EOL;
160
            $str = $this->inputsHTML($type, $key, $str).PHP_EOL;
161
            $str .='@error("'.$key.'") <div class="invalid-feedback">{{ $message }}</div> @enderror</div>'.PHP_EOL;
162
        }
163
164
        return $str;
165
    }
166
167
    protected function inputsHTML($type, $key, string $str): string
168
    {
169
        $array = [
170
            'text' => '<input type="text" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL,
171
            'email' => '<input type="email" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL,
172
            'number' => '<input type="number" wire:model.lazy="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL,
173
            'file' => '<input type="file" wire:model="' . $key . '" class="form-control-file @error(\''.$key.'\')is-invalid @enderror" id="input' . $key . '"">'.PHP_EOL,
174
            'textarea' => '<textarea wire:model="' . $key . '" class="form-control @error(\''.$key.'\')is-invalid @enderror"></textarea>'.PHP_EOL,
175
            'password' => '<input type="password" wire:model.lazy="' . $key . '" class="form-control  @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">'.PHP_EOL,
176
        ];
177
        $str .= $array[$type];
178
179
        return $str;
180
    }
181
182
    private function makeTab($count){
183
        $count = $count * 4;
184
        $tabs = str_repeat(' ', $count);
185
186
        return "\n".$tabs;
187
    }
188
189
}
190