1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Enum\Laravel\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class MakeEnum extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
protected $name = 'make:enum'; |
13
|
|
|
protected $description = 'Create a new enum'; |
14
|
|
|
|
15
|
36 |
|
protected function getStub() |
16
|
|
|
{ |
17
|
36 |
|
return __DIR__.'/../../stubs/enum.stub'; |
18
|
|
|
} |
19
|
|
|
|
20
|
36 |
|
protected function getDefaultNamespace($rootNamespace) |
21
|
|
|
{ |
22
|
36 |
|
return $rootNamespace.'\Enums'; |
23
|
|
|
} |
24
|
|
|
|
25
|
36 |
|
protected function replaceClass($stub, $name) |
26
|
|
|
{ |
27
|
36 |
|
$stub = parent::replaceClass($stub, $name); |
28
|
|
|
|
29
|
36 |
|
$stub = str_replace('DummyDocBlock', $this->getDocBlock(), $stub); |
30
|
36 |
|
$stub = str_replace('DummyValueMapConst', $this->getValueMapConst(), $stub); |
31
|
|
|
|
32
|
36 |
|
return $stub; |
33
|
|
|
} |
34
|
|
|
|
35
|
36 |
|
protected function getDocBlock(): string |
36
|
|
|
{ |
37
|
36 |
|
$methods = array_merge($this->option('method'), $this->option('value')); |
38
|
|
|
|
39
|
36 |
|
if (! empty($methods)) { |
40
|
32 |
|
$docBlock = PHP_EOL.'/**'; |
41
|
|
|
$docBlock .= implode('', array_map(function ($method) { |
42
|
32 |
|
return PHP_EOL.' * @method static self '.$this->formatValueToMethod($method).'()'; |
43
|
32 |
|
}, $methods)); |
44
|
32 |
|
$docBlock .= PHP_EOL.' */'; |
45
|
|
|
} |
46
|
|
|
|
47
|
36 |
|
return $docBlock ?? ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
36 |
|
protected function getValueMapConst(): string |
51
|
|
|
{ |
52
|
36 |
|
$values = $this->option('value'); |
53
|
|
|
|
54
|
36 |
|
if (! empty($values)) { |
55
|
16 |
|
$tab = str_repeat(' ', 4); |
56
|
16 |
|
$constant = $tab.'const MAP_VALUE = ['; |
57
|
|
|
|
58
|
16 |
|
foreach ($values as $value) { |
|
|
|
|
59
|
16 |
|
$constant .= PHP_EOL.$tab.$tab.'\''.$this->formatValueToMethod($value).'\' => \''.$value.'\','; |
60
|
|
|
} |
61
|
|
|
|
62
|
16 |
|
$constant .= PHP_EOL.$tab.'];'; |
63
|
|
|
} |
64
|
|
|
|
65
|
36 |
|
return $constant ?? ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
32 |
|
protected function formatValueToMethod(string $value): string |
69
|
|
|
{ |
70
|
32 |
|
switch ($this->option('formatter')) { |
71
|
32 |
|
case 'const': |
72
|
8 |
|
return strtoupper(Str::snake($value)); |
73
|
24 |
|
case 'snake': |
74
|
8 |
|
return Str::snake($value); |
75
|
16 |
|
case 'studly': |
76
|
8 |
|
return Str::studly($value); |
77
|
8 |
|
case 'camel': |
78
|
|
|
default: |
79
|
8 |
|
return Str::camel($value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
36 |
|
protected function getArguments() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
36 |
|
['name', InputArgument::REQUIRED, 'The name of the enum'], |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
36 |
|
protected function getOptions() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
36 |
|
['method', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The method name that should be added to the enum'], |
94
|
36 |
|
['value', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The value that should be added to the enum'], |
95
|
36 |
|
['formatter', null, InputOption::VALUE_REQUIRED, 'The formatter to use for the value to method conversion (snake, const, studly, camel)', 'camel'], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function handle() |
100
|
|
|
{ |
101
|
|
|
$this->type = $this->getNameInput(); |
102
|
|
|
|
103
|
|
|
return parent::handle(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.