-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* composer.json * src/HasEnums.php * tests/TestCase.php
- Loading branch information
Showing
21 changed files
with
906 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,7 @@ preset: laravel | |
disabled: | ||
- single_class_element_per_statement | ||
- self_accessor | ||
|
||
finder: | ||
exclude: | ||
- "tests/stubs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Commands; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Console\GeneratorCommand; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class MakeEnum extends GeneratorCommand | ||
{ | ||
protected $name = 'make:enum'; | ||
protected $description = 'Create a new enum'; | ||
|
||
protected function getStub() | ||
{ | ||
return __DIR__.'/../../stubs/enum.stub'; | ||
} | ||
|
||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
return $rootNamespace.'\Enums'; | ||
} | ||
|
||
protected function replaceClass($stub, $name) | ||
{ | ||
$stub = parent::replaceClass($stub, $name); | ||
|
||
$stub = str_replace('DummyDocBlock', $this->getDocBlock(), $stub); | ||
$stub = str_replace('DummyValueMapConst', $this->getValueMapConst(), $stub); | ||
|
||
return $stub; | ||
} | ||
|
||
protected function getDocBlock(): string | ||
{ | ||
$methods = array_merge($this->option('method'), $this->option('value')); | ||
|
||
if (! empty($methods)) { | ||
$docBlock = PHP_EOL.'/**'; | ||
$docBlock .= implode('', array_map(function ($method) { | ||
return PHP_EOL.' * @method static self '.$this->formatValueToMethod($method).'()'; | ||
}, $methods)); | ||
$docBlock .= PHP_EOL.' */'; | ||
} | ||
|
||
return $docBlock ?? ''; | ||
} | ||
|
||
protected function getValueMapConst(): string | ||
{ | ||
$values = $this->option('value'); | ||
|
||
if (! empty($values)) { | ||
$tab = str_repeat(' ', 4); | ||
$constant = $tab.'const MAP_VALUE = ['; | ||
|
||
foreach ($values as $value) { | ||
$constant .= PHP_EOL.$tab.$tab.'\''.$this->formatValueToMethod($value).'\' => \''.$value.'\','; | ||
} | ||
|
||
$constant .= PHP_EOL.$tab.'];'; | ||
} | ||
|
||
return $constant ?? ''; | ||
} | ||
|
||
protected function formatValueToMethod(string $value): string | ||
{ | ||
switch ($this->option('formatter')) { | ||
case 'const': | ||
return strtoupper(Str::snake($value)); | ||
case 'snake': | ||
return Str::snake($value); | ||
case 'studly': | ||
return Str::studly($value); | ||
case 'camel': | ||
default: | ||
return Str::camel($value); | ||
} | ||
} | ||
|
||
protected function getArguments() | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the enum'], | ||
]; | ||
} | ||
|
||
protected function getOptions() | ||
{ | ||
return [ | ||
['method', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The method name that should be added to the enum'], | ||
['value', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The value that should be added to the enum'], | ||
['formatter', null, InputOption::VALUE_REQUIRED, 'The formatter to use for the value to method conversion (snake, const, studly, camel)', 'camel'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Spatie\Enum\Laravel\Commands\MakeEnum; | ||
|
||
class EnumServiceProvider extends ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->app->bind('command.make:enum', MakeEnum::class); | ||
|
||
$this->commands([ | ||
'command.make:enum', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class NoSuchEnumField extends InvalidArgumentException | ||
{ | ||
public static function make(string $field, string $model): NoSuchEnumField | ||
{ | ||
return new self("No enum field {$field} registered on model {$model}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace DummyNamespace; | ||
|
||
use Spatie\Enum\Enum; | ||
DummyDocBlock | ||
final class DummyClass extends Enum | ||
{ | ||
DummyValueMapConst | ||
} |
Oops, something went wrong.