diff --git a/CHANGELOG.md b/CHANGELOG.md index 018be38..fa7397c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `laravel-enum` will be documented in this file: +## 1.1.0 - 2021-03-17 + +Added: +* added Artisan `make:enum` command for enumeration classes generation + ## 1.0.0 - 2021-03-08 -- initial release +Added: +* initial release diff --git a/README.md b/README.md index 1762314..aa4bbd3 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,34 @@ dump($hero->character); // -value: "evil" // } ``` + +### Enumeration class generation + +Package provides `make:enum` Artisan command for enumeration classes auto-generation. To generate new enum class, run: +```php +php artisan make:enum Character --cases='good,evil,sometimes_good_sometimes_evil' +``` +> `--cases` (or `-c`) option allows defining available enum cases. Command can be run without that option specified. + +Above command will create a new class inside `Enums` directory: +```php +namespace App\Enums; + +use DeSmart\Laravel\Enumeration\Enumeration; + +/** + * @method static Character good() + * @method static Character evil() + * @method static Character sometimesGoodSometimesEvil() + */ +class Character extends Enumeration +{ + const GOOD = 'good'; + const EVIL = 'evil'; + const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil'; +} +``` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/composer.json b/composer.json index 099ae35..d7eddc0 100755 --- a/composer.json +++ b/composer.json @@ -37,6 +37,13 @@ "preferred-install": "dist", "sort-packages": true }, + "extra": { + "laravel": { + "providers": [ + "DeSmart\\Laravel\\Enumeration\\EnumerationServiceProvider" + ] + } + }, "minimum-stability": "dev", "prefer-stable": true } diff --git a/src/Console/MakeEnumCommand.php b/src/Console/MakeEnumCommand.php new file mode 100644 index 0000000..f8bb6a9 --- /dev/null +++ b/src/Console/MakeEnumCommand.php @@ -0,0 +1,103 @@ +getNamespace($name) . '\\', '', $name); + + $staticMethods = "\n"; + $constants = ''; + + if ($this->option('cases')) { + $cases = array_map('trim', explode(',', $this->option('cases'))); + + $staticMethods .= $this->generateStaticMethods($cases, $class); + $constants .= $this->generateConstants($cases); + } + + $constants .= '}'; + + return str_replace( + ['{{ staticMethods }}', '{{ constants }}', '{{ class }}'], + [$staticMethods, $constants, $class], + $stub + ); + } + + protected function generateStaticMethods(array $cases, string $class): string + { + $staticMethods = "/**\n"; + + foreach ($cases as $case) { + $method = Str::camel(Str::lower($case)); + $staticMethods .= " * @method static $class $method()\n"; + } + + $staticMethods .= " */\n"; + + return $staticMethods; + } + + protected function generateConstants(array $cases): string + { + $constants = ''; + + foreach ($cases as $case) { + $constant = toConstName($case); + $constants .= "\tconst $constant = '$case';\n"; + } + + return $constants; + } + + protected function getStub(): string + { + return __DIR__ . '/stubs/enum.stub'; + } + + /** + * @param string $rootNamespace + * @return string + */ + protected function getDefaultNamespace($rootNamespace): string + { + return $rootNamespace . '\\Enums'; + } + + protected function getOptions(): array + { + return [ + ['cases', '-c', InputOption::VALUE_OPTIONAL, 'Set enum cases'], + ]; + } +} diff --git a/src/Console/stubs/enum.stub b/src/Console/stubs/enum.stub new file mode 100644 index 0000000..db8c3ec --- /dev/null +++ b/src/Console/stubs/enum.stub @@ -0,0 +1,8 @@ +app->runningInConsole()) { + $this->commands(MakeEnumCommand::class); + } + } +} \ No newline at end of file