-
Notifications
You must be signed in to change notification settings - Fork 3
Add make command #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add make command #40
Changes from 14 commits
63db580
9d1c2ba
1f26573
478c2d3
bf3487b
299be5a
ada774b
c6c5433
d71dbdb
4d6d63f
f49cc32
7af08f2
18ce1a7
d775c5c
6b9c36d
f379e1e
dcecfb9
cb81515
bfc9079
3c3fc77
bb27038
146b772
b71f137
71d8a5b
0b3599c
4e5db9c
5064e73
68a5032
bb3e9c3
fb12a1d
554e8d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Webfox\LaravelBackedEnums; | ||
|
||
use InvalidArgumentException; | ||
use Illuminate\Foundation\Console\EnumMakeCommand; | ||
|
||
|
||
class LaravelBackedEnumMakeCommand extends EnumMakeCommand | ||
{ | ||
protected $description = 'Create a new laravel backed enum'; | ||
|
||
protected function getStub(): string | ||
{ | ||
if ($this->option('string') || $this->option('int')) { | ||
Check failure on line 15 in src/LaravelBackedEnumMakeCommand.php
|
||
return $this->resolveStubPath('/stubs/laravel-backed-enum.stub'); | ||
} | ||
return parent::getStub(); | ||
Check failure on line 18 in src/LaravelBackedEnumMakeCommand.php
|
||
} | ||
|
||
protected function buildClass($name): array|string | ||
{ | ||
if ($this->option('string') || $this->option('int')) { | ||
Check failure on line 23 in src/LaravelBackedEnumMakeCommand.php
|
||
return str_replace( | ||
['{{ value }}'], | ||
$this->option('string') ? '\'standard\'' : '0', | ||
parent::buildClass($name) | ||
Check failure on line 27 in src/LaravelBackedEnumMakeCommand.php
|
||
); | ||
} | ||
return parent::buildClass($name); | ||
Check failure on line 30 in src/LaravelBackedEnumMakeCommand.php
|
||
} | ||
|
||
|
||
protected function resolveStubPath($stub): string | ||
{ | ||
|
||
if (file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))) { | ||
return $customPath; | ||
} | ||
|
||
if (file_exists(__DIR__ . "/../" . $stub)) { | ||
return __DIR__ . "/../" . $stub; | ||
} | ||
|
||
return parent::resolveStubPath($stub); | ||
} | ||
|
||
protected function getNameInput(): string | ||
{ | ||
$name = trim($this->argument('name')); | ||
if (!preg_match('/^[A-Za-z_\x7f-\xff][A-Za-z0-9_\x7f-\xff]*$/', $name)) { | ||
throw new InvalidArgumentException('Invalid enum name format'); | ||
} | ||
|
||
if (str_ends_with($name, 'Enum')) { | ||
return $name; | ||
} | ||
|
||
return $name . 'Enum'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use Webfox\LaravelBackedEnums\BackedEnum; | ||
use Webfox\LaravelBackedEnums\IsBackedEnum; | ||
|
||
enum {{ class }}: {{ type }} implements BackedEnum | ||
{ | ||
use IsBackedEnum; | ||
|
||
/** | ||
* Add your Enums below using. | ||
* e.g. case Standard = {{ value }}; | ||
*/ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Enums; | ||
|
||
use Webfox\LaravelBackedEnums\BackedEnum; | ||
use Webfox\LaravelBackedEnums\IsBackedEnum; | ||
|
||
enum IntEnum: int implements BackedEnum | ||
{ | ||
use IsBackedEnum; | ||
|
||
/** | ||
* Add your Enums below using. | ||
* e.g. case Standard = 0; | ||
*/ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Enums; | ||
|
||
enum PureEnum | ||
{ | ||
// | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Enums; | ||
|
||
use Webfox\LaravelBackedEnums\BackedEnum; | ||
use Webfox\LaravelBackedEnums\IsBackedEnum; | ||
|
||
enum StringEnum: string implements BackedEnum | ||
{ | ||
use IsBackedEnum; | ||
|
||
/** | ||
* Add your Enums below using. | ||
* e.g. case Standard = 'standard'; | ||
*/ | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.