Skip to content

Commit 519196f

Browse files
authored
Add make command
Added make command for easy enum creation
1 parent 1c02cde commit 519196f

16 files changed

+294
-80
lines changed

.github/workflows/phpstan.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

.github/workflows/run-tests.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ name: run-tests
22

33
on:
44
push:
5-
branches: [main]
5+
branches: [ main ]
66
pull_request:
7-
branches: [main]
8-
workflow_dispatch:
7+
branches: [ main ]
98

109
jobs:
1110
test:
1211
runs-on: ${{ matrix.os }}
1312
strategy:
1413
fail-fast: true
1514
matrix:
16-
os: [ubuntu-latest]
17-
php: [8.2]
18-
laravel: [11.*]
19-
stability: [prefer-lowest, prefer-stable]
15+
os: [ ubuntu-latest ]
16+
php: [ 8.2 ]
17+
laravel: [ 10.*,11.* ]
2018

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

@@ -38,6 +36,7 @@ jobs:
3836
3937
- name: Install dependencies
4038
run: |
39+
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
4140
composer install
4241
4342
- name: Execute tests

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ phpstan.neon
99
testbench.yaml
1010
vendor
1111
node_modules
12+
/workbench/app/Enums/

README.md

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,45 @@ composer require webfox/laravel-backed-enums
1313

1414
## Usage
1515

16-
### Setup your enum
16+
### Make Command
17+
18+
Creating a new Laravel Backed Enum is easy with the make:enum command.
19+
20+
#### Command:
21+
22+
```Bash
23+
php artisan make:enum {name} --string # or --int
24+
```
25+
26+
#### Arguments:
27+
28+
* `{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).
29+
* `{type?}`: The underlying data type for the enum. Can be either --int --string or if not specified it will be a pure enum.
30+
* `{--force}`: Overwrite the enum if it already exists.
31+
Example Usage:
32+
33+
To create an enum named OrderStatusEnum backed by integers:
34+
35+
``` Bash
36+
php artisan make:enum OrderStatus --int
37+
```
38+
39+
To create an enum named OrderStatusEnum backed by strings:
40+
41+
``` Bash
42+
php artisan make:enum OrderStatus --string
43+
```
44+
45+
To create a pure enum named OrderStatusEnum:
46+
47+
``` Bash
48+
php artisan make:enum OrderStatus
49+
50+
```
51+
52+
This will generate an OrderStatusEnums in the `app/Enums` directory.
53+
54+
### Upgrade your existing enums
1755

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

318356
#### Usage
357+
319358
```
320359
public function rules(): array
321360
{
@@ -328,34 +367,41 @@ public function rules(): array
328367
## Other Classes
329368

330369
### AsFullEnumCollection
331-
This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built-in will maintain the full `toArray` structure
370+
371+
This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built-in will maintain the full `toArray` structure
332372
when converting to json.
333373

334374
E.g. the Laravel built in `AsEnumCollection` cast will return the following json:
375+
335376
```json
336-
["MILLIGRAMS", "GRAMS"]
377+
[
378+
"MILLIGRAMS",
379+
"GRAMS"
380+
]
337381
```
382+
338383
This cast will return
384+
339385
```json
340386
[
341-
{
342-
"name": "MILLIGRAMS",
343-
"value": "MILLIGRAMS",
344-
"label": "mg",
345-
"meta": {
346-
"background_color": "bg-green-100",
347-
"text_color": "text-green-800"
348-
}
349-
},
350-
{
351-
"name": "GRAMS",
352-
"value": "GRAMS",
353-
"label": "g",
354-
"meta": {
355-
"background_color": "bg-red-100",
356-
"text_color": "text-red-800"
387+
{
388+
"name": "MILLIGRAMS",
389+
"value": "MILLIGRAMS",
390+
"label": "mg",
391+
"meta": {
392+
"background_color": "bg-green-100",
393+
"text_color": "text-green-800"
394+
}
395+
},
396+
{
397+
"name": "GRAMS",
398+
"value": "GRAMS",
399+
"label": "g",
400+
"meta": {
401+
"background_color": "bg-red-100",
402+
"text_color": "text-red-800"
403+
}
357404
}
358-
}
359405
]
360406
```
361407

composer.json

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"spatie/laravel-package-tools": "^1.14.0",
21-
"illuminate/contracts": "^10.0|^11.0"
20+
"illuminate/console": "^10.0 | ^11.0",
21+
"spatie/laravel-package-tools": "^1.18",
22+
"illuminate/contracts": "^10.0 | ^11.0"
2223
},
2324
"require-dev": {
24-
"laravel/pint": "^1.0",
25-
"nunomaduro/collision": "^6.1",
26-
"larastan/larastan": "^v2.9.2",
27-
"orchestra/testbench": "^8.0",
28-
"pestphp/pest": "^1.21",
29-
"pestphp/pest-plugin-laravel": "^1.1",
30-
"phpstan/extension-installer": "^1.1",
31-
"phpstan/phpstan-deprecation-rules": "^1.0",
32-
"phpstan/phpstan-phpunit": "^1.0",
33-
"phpunit/phpunit": "^9.6"
25+
"laravel/pint": "^1.20",
26+
"nunomaduro/collision": "^7.11 | ^8.5",
27+
"pestphp/pest": "^2.36 | ^3.7",
28+
"pestphp/pest-plugin-laravel": "^2.4 | ^3.0",
29+
"orchestra/testbench": "^8.31 | ^9.9",
30+
"phpunit/phpunit": "^10.5 | ^11.5",
31+
"phpstan/extension-installer": "^1.4",
32+
"larastan/larastan": "^2.9 | ^3.0",
33+
"phpstan/phpstan-phpunit": "^1.4 | ^2.0",
34+
"phpstan/phpstan-deprecation-rules": "^1.2 | ^2.0"
3435
},
3536
"autoload": {
3637
"psr-4": {
@@ -83,4 +84,4 @@
8384
},
8485
"minimum-stability": "dev",
8586
"prefer-stable": true
86-
}
87+
}

phpstan-baseline.neon

Whitespace-only changes.

phpstan.neon.dist

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/IsBackedEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/**
1414
* @implements \Webfox\LaravelBackedEnums\BackedEnum<string,string>
1515
* @mixin \BackedEnum<string,string>
16+
* @phpstan-ignore trait.unused
1617
*/
1718
trait IsBackedEnum
1819
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Webfox\LaravelBackedEnums;
4+
5+
use InvalidArgumentException;
6+
use Illuminate\Foundation\Console\EnumMakeCommand;
7+
8+
class LaravelBackedEnumMakeCommand extends EnumMakeCommand
9+
{
10+
protected $description = 'Create a new laravel backed enum';
11+
12+
protected function getStub(): string
13+
{
14+
if ($this->option('string') || $this->option('int')) {
15+
return $this->resolveStubPath('/stubs/laravel-backed-enum.stub');
16+
}
17+
return parent::getStub();
18+
}
19+
20+
protected function buildClass($name): array|string
21+
{
22+
if ($this->option('string') || $this->option('int')) {
23+
return str_replace(
24+
['{{ value }}'],
25+
$this->option('string') ? '\'standard\'' : '0',
26+
parent::buildClass($name)
27+
);
28+
}
29+
return parent::buildClass($name);
30+
}
31+
32+
33+
protected function resolveStubPath($stub): string
34+
{
35+
36+
if (file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))) {
37+
return $customPath;
38+
}
39+
40+
if (file_exists(__DIR__ . "/../" . $stub)) {
41+
return __DIR__ . "/../" . $stub;
42+
}
43+
44+
return parent::resolveStubPath($stub);
45+
}
46+
47+
protected function getNameInput(): string
48+
{
49+
$name = trim($this->argument('name'));
50+
if (!preg_match('/^[A-Za-z_\x7f-\xff][A-Za-z0-9_\x7f-\xff]*$/', $name)) {
51+
throw new InvalidArgumentException('Invalid enum name format');
52+
}
53+
54+
if (str_ends_with($name, 'Enum')) {
55+
return $name;
56+
}
57+
58+
return $name . 'Enum';
59+
}
60+
}

src/LaravelBackedEnumsServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ public function configurePackage(Package $package): void
1717
$package
1818
->name('laravel-backed-enums')
1919
->hasConfigFile();
20+
21+
22+
/** @noinspection PhpFullyQualifiedNameUsageInspection */
23+
if (class_exists(\Illuminate\Foundation\Console\EnumMakeCommand::class)) {
24+
$package->hasConsoleCommand(LaravelBackedEnumMakeCommand::class);
25+
}
2026
}
2127
}

0 commit comments

Comments
 (0)