Skip to content

Enum class generation command #1

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

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"DeSmart\\Laravel\\Enumeration\\EnumerationServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
103 changes: 103 additions & 0 deletions src/Console/MakeEnumCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace DeSmart\Laravel\Enumeration\Console;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;
use function DeSmart\Enum\Helpers\toConstName;

class MakeEnumCommand extends GeneratorCommand
{
/**
* @var string
*/
protected $name = 'make:enum';

/**
* @var string
*/
protected $description = 'Create a new Enumeration class';

/**
* @var string
*/
protected $type = 'Enum';

/**
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceClass($stub, $name): string
{
$class = str_replace($this->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'],
];
}
}
8 changes: 8 additions & 0 deletions src/Console/stubs/enum.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {{ namespace }};

use DeSmart\Laravel\Enumeration\Enumeration;
{{ staticMethods }}class {{ class }} extends Enumeration
{
{{ constants }}
18 changes: 18 additions & 0 deletions src/EnumerationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DeSmart\Laravel\Enumeration;

use DeSmart\Laravel\Enumeration\Console\MakeEnumCommand;
use Illuminate\Support\ServiceProvider;

class EnumerationServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->commands(MakeEnumCommand::class);
}
}
}