Skip to content

Commit

Permalink
Add make command
Browse files Browse the repository at this point in the history
Added make command for easy enum creation
  • Loading branch information
Jim-Webfox authored Jan 21, 2025
1 parent 1c02cde commit 519196f
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 80 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/phpstan.yml

This file was deleted.

13 changes: 6 additions & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ name: run-tests

on:
push:
branches: [main]
branches: [ main ]
pull_request:
branches: [main]
workflow_dispatch:
branches: [ main ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.2]
laravel: [11.*]
stability: [prefer-lowest, prefer-stable]
os: [ ubuntu-latest ]
php: [ 8.2 ]
laravel: [ 10.*,11.* ]

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand All @@ -38,6 +36,7 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer install
- name: Execute tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
/workbench/app/Enums/
86 changes: 66 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,45 @@ composer require webfox/laravel-backed-enums

## Usage

### Setup your enum
### Make Command

Creating a new Laravel Backed Enum is easy with the make:enum command.

#### Command:

```Bash
php artisan make:enum {name} --string # or --int
```

#### Arguments:

* `{name}`: The name of the enum class to be created (e.g., OrderStatus). The command will automatically append "Enum" to the name (e.g., OrderStatusEnum).
* `{type?}`: The underlying data type for the enum. Can be either --int --string or if not specified it will be a pure enum.
* `{--force}`: Overwrite the enum if it already exists.
Example Usage:

To create an enum named OrderStatusEnum backed by integers:

``` Bash
php artisan make:enum OrderStatus --int
```

To create an enum named OrderStatusEnum backed by strings:

``` Bash
php artisan make:enum OrderStatus --string
```

To create a pure enum named OrderStatusEnum:

``` Bash
php artisan make:enum OrderStatus

```

This will generate an OrderStatusEnums in the `app/Enums` directory.

### Upgrade your existing enums

The enum you create must implement the `BackedEnum` interface and also use the `IsBackedEnum` trait.
The interface is required for Laravel to cast your enum correctly and the trait is what gives your enum its superpowers.
Expand Down Expand Up @@ -316,6 +354,7 @@ The backed enums may be validated using Laravel's standard Enum validation rule
This method a shortcut for the validation rule.

#### Usage

```
public function rules(): array
{
Expand All @@ -328,34 +367,41 @@ public function rules(): array
## Other Classes

### AsFullEnumCollection
This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built-in will maintain the full `toArray` structure

This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built-in will maintain the full `toArray` structure
when converting to json.

E.g. the Laravel built in `AsEnumCollection` cast will return the following json:

```json
["MILLIGRAMS", "GRAMS"]
[
"MILLIGRAMS",
"GRAMS"
]
```

This cast will return

```json
[
{
"name": "MILLIGRAMS",
"value": "MILLIGRAMS",
"label": "mg",
"meta": {
"background_color": "bg-green-100",
"text_color": "text-green-800"
}
},
{
"name": "GRAMS",
"value": "GRAMS",
"label": "g",
"meta": {
"background_color": "bg-red-100",
"text_color": "text-red-800"
{
"name": "MILLIGRAMS",
"value": "MILLIGRAMS",
"label": "mg",
"meta": {
"background_color": "bg-green-100",
"text_color": "text-green-800"
}
},
{
"name": "GRAMS",
"value": "GRAMS",
"label": "g",
"meta": {
"background_color": "bg-red-100",
"text_color": "text-red-800"
}
}
}
]
```

Expand Down
27 changes: 14 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0|^11.0"
"illuminate/console": "^10.0 | ^11.0",
"spatie/laravel-package-tools": "^1.18",
"illuminate/contracts": "^10.0 | ^11.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.1",
"larastan/larastan": "^v2.9.2",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.6"
"laravel/pint": "^1.20",
"nunomaduro/collision": "^7.11 | ^8.5",
"pestphp/pest": "^2.36 | ^3.7",
"pestphp/pest-plugin-laravel": "^2.4 | ^3.0",
"orchestra/testbench": "^8.31 | ^9.9",
"phpunit/phpunit": "^10.5 | ^11.5",
"phpstan/extension-installer": "^1.4",
"larastan/larastan": "^2.9 | ^3.0",
"phpstan/phpstan-phpunit": "^1.4 | ^2.0",
"phpstan/phpstan-deprecation-rules": "^1.2 | ^2.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -83,4 +84,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
Empty file removed phpstan-baseline.neon
Empty file.
13 changes: 0 additions & 13 deletions phpstan.neon.dist

This file was deleted.

1 change: 1 addition & 0 deletions src/IsBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/**
* @implements \Webfox\LaravelBackedEnums\BackedEnum<string,string>
* @mixin \BackedEnum<string,string>
* @phpstan-ignore trait.unused
*/
trait IsBackedEnum
{
Expand Down
60 changes: 60 additions & 0 deletions src/LaravelBackedEnumMakeCommand.php
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';
}
}
6 changes: 6 additions & 0 deletions src/LaravelBackedEnumsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ public function configurePackage(Package $package): void
$package
->name('laravel-backed-enums')
->hasConfigFile();


/** @noinspection PhpFullyQualifiedNameUsageInspection */
if (class_exists(\Illuminate\Foundation\Console\EnumMakeCommand::class)) {
$package->hasConsoleCommand(LaravelBackedEnumMakeCommand::class);
}
}
}
17 changes: 17 additions & 0 deletions stubs/laravel-backed-enum.stub
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 }};
*/

}
17 changes: 17 additions & 0 deletions tests/Fixtures/ExpectedIntEnum.php
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;
*/

}
8 changes: 8 additions & 0 deletions tests/Fixtures/ExpectedPureEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Workbench\App\Enums;

enum PureEnum
{
//
}
17 changes: 17 additions & 0 deletions tests/Fixtures/ExpectedStringEnum.php
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';
*/

}
Loading

0 comments on commit 519196f

Please sign in to comment.