generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added make command for easy enum creation
- Loading branch information
1 parent
1c02cde
commit 519196f
Showing
16 changed files
with
294 additions
and
80 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ phpstan.neon | |
testbench.yaml | ||
vendor | ||
node_modules | ||
/workbench/app/Enums/ |
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
Empty file.
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
<?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')) { | ||
return $this->resolveStubPath('/stubs/laravel-backed-enum.stub'); | ||
} | ||
return parent::getStub(); | ||
} | ||
|
||
protected function buildClass($name): array|string | ||
{ | ||
if ($this->option('string') || $this->option('int')) { | ||
return str_replace( | ||
['{{ value }}'], | ||
$this->option('string') ? '\'standard\'' : '0', | ||
parent::buildClass($name) | ||
); | ||
} | ||
return parent::buildClass($name); | ||
} | ||
|
||
|
||
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'; | ||
} | ||
} |
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,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 }}; | ||
*/ | ||
|
||
} |
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,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; | ||
*/ | ||
|
||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Enums; | ||
|
||
enum PureEnum | ||
{ | ||
// | ||
} |
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,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'; | ||
*/ | ||
|
||
} |
Oops, something went wrong.