Completed
Push — master ( 47c6ec...2934e3 )
by Arjay
06:39
created

DataTablesMakeCommand::parseArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace Yajra\DataTables\Generators;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class DataTablesMakeCommand extends GeneratorCommand
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'datatables:make
17
                            {name : The name of the datatable.}
18
                            {--model : The name of the model to be used.}
19
                            {--model-namespace= : The namespace of the model to be used.}
20
                            {--action= : The path of the action view.}
21
                            {--columns= : The columns of the datatable.}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Create a new DataTable service class.';
29
30
    /**
31
     * The type of class being generated.
32
     *
33
     * @var string
34
     */
35
    protected $type = 'DataTable';
36
37
    /**
38
     * Build the class with the given name.
39
     *
40
     * @param  string $name
41
     * @return string
42
     */
43
    protected function buildClass($name)
44
    {
45
        $stub = parent::buildClass($name);
46
47
        return $this->replaceModelImport($stub)
48
                    ->replaceModel($stub)
49
                    ->replaceColumns($stub)
50
                    ->replaceAction($stub)
51
                    ->replaceFilename($stub);
52
    }
53
54
    /**
55
     * Replace model name.
56
     *
57
     * @param string $stub
58
     * @return \Yajra\DataTables\Generators\DataTablesMakeCommand
59
     */
60
    protected function replaceModel(&$stub)
61
    {
62
        $model = explode('\\', $this->getModel());
63
        $model = array_pop($model);
64
        $stub  = str_replace('ModelName', $model, $stub);
65
66
        return $this;
67
    }
68
69
    /**
70
     * Get model name to use.
71
     */
72
    protected function getModel()
73
    {
74
        $name           = $this->getNameInput();
75
        $rootNamespace  = $this->laravel->getNamespace();
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $model = $this->option('model') || $this->option('model-namespace');
77
        $modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model');
78
79
        return $model
80
            ? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") .  str_singular($name)
81
            : $rootNamespace . "\\User";
82
    }
83
84
    /**
85
     * Replace columns.
86
     *
87
     * @param string $stub
88
     * @return $this
89
     */
90
    protected function replaceColumns(&$stub)
91
    {
92
        $stub = str_replace(
93
            'DummyColumns', $this->getColumns(), $stub
94
        );
95
96
        return $this;
97
    }
98
99
    /**
100
     * Get the columns to be used.
101
     *
102
     * @return string
103
     */
104
    protected function getColumns()
105
    {
106
        if($this->option('columns') != ''){
107
            return $this->parseArray($this->option('columns'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('columns') targeting Illuminate\Console\Command::option() can also be of type array; however, Yajra\DataTables\Generat...keCommand::parseArray() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
108
        }else{
109
            return $this->parseArray('id,add your columns,created_at,updated_at');
110
        }
111
    }
112
113
    /**
114
     * Parse array from definition
115
     *
116
     * @param  string  $definition
117
     * @param  string  $delimiter
118
     * @param  int     $indentation
119
     * @return string
120
     */
121
    protected function parseArray($definition, $delimiter = ',', $indentation = 12)
122
    {
123
        return str_replace($delimiter, "',\n" . str_repeat(' ', $indentation) . "'", $definition);
124
    }
125
126
    /**
127
     * Replace the action.
128
     *
129
     * @param string $stub
130
     * @return \Yajra\DataTables\Generators\DataTablesMakeCommand
131
     */
132
    protected function replaceAction(&$stub)
133
    {
134
        $stub = str_replace(
135
            'DummyAction', $this->getAction(), $stub
136
        );
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set the action view to be used.
143
     *
144
     * @return string
145
     */
146
    protected function getAction()
147
    {
148
        return $this->option('action') ? $this->option('action') : Str::lower($this->getNameInput()) . '.action';
149
150
    }
151
152
    /**
153
     * Replace model import.
154
     *
155
     * @param string $stub
156
     * @return $this
157
     */
158
    protected function replaceModelImport(&$stub)
159
    {
160
        $stub = str_replace(
161
            'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub
162
        );
163
164
        return $this;
165
    }
166
167
    /**
168
     * Get the stub file for the generator.
169
     *
170
     * @return string
171
     */
172
    protected function getStub()
173
    {
174
        $config = $this->laravel['config'];
175
        return $config->get('datatables-buttons.stub')
176
            ? base_path() . $config->get('datatables-buttons.stub') . '/datatables.stub'
177
            : __DIR__ . '/stubs/datatables.stub';
178
    }
179
180
    /**
181
     * Replace the filename.
182
     *
183
     * @param string $stub
184
     * @return string
185
     */
186
    protected function replaceFilename(&$stub)
187
    {
188
        $stub = str_replace(
189
            'DummyFilename', str_slug($this->getNameInput()), $stub
190
        );
191
192
        return $stub;
193
    }
194
195
    /**
196
     * Get the console command options.
197
     *
198
     * @return array
199
     */
200
    protected function getOptions()
201
    {
202
        return [
203
            ['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null],
204
            ['action', null, InputOption::VALUE_OPTIONAL, 'Path to action column template.', null],
205
            ['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null],
206
        ];
207
    }
208
209
    /**
210
     * Parse the name and format according to the root namespace.
211
     *
212
     * @param  string $name
213
     * @return string
214
     */
215
    protected function qualifyClass($name)
216
    {
217
        $rootNamespace = $this->laravel->getNamespace();
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
219
        if (Str::startsWith($name, $rootNamespace)) {
220
            return $name;
221
        }
222
223
        if (Str::contains($name, '/')) {
224
            $name = str_replace('/', '\\', $name);
225
        }
226
227
        if (! Str::contains(Str::lower($name), 'datatable')) {
228
            $name .= 'DataTable';
229
        }
230
231
        return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
232
    }
233
234
    /**
235
     * Get the default namespace for the class.
236
     *
237
     * @param  string $rootNamespace
238
     * @return string
239
     */
240
    protected function getDefaultNamespace($rootNamespace)
241
    {
242
        return $rootNamespace . "\\" . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables');
243
    }
244
}
245