Skip to content

Commit bf3487b

Browse files
committed
MOD changes as per comments from code rabbit
1 parent 478c2d3 commit bf3487b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@ composer require webfox/laravel-backed-enums
1515

1616
### Make Command
1717

18-
You can use the `make:laravel-backed-enum` command to create a new enum.
18+
Conveniently create a new backed enum using the make command.
1919

20-
```bash
20+
#### Command:
21+
22+
```Bash
2123
php artisan make:laravel-backed-enum {name} {enumType}
2224
```
2325

24-
This will create a new enum in the `app/Enums` directory.
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+
* `{enumType}`: The underlying data type for the enum. Can be either int or string (defaults to string if not specified).
30+
Example Usage:
2531

26-
The command takes in two arguments:
32+
To create an enum named OrderStatusEnum backed by integers:
33+
34+
``` Bash
35+
php artisan make:laravel-backed-enum OrderStatus int
36+
```
2737

28-
- name: The name of the enum
29-
- enumType: The type of the enum. This can be either `string` or `int`. Default is `string`.
38+
This will generate an OrderStatusEnum class in the `app/Enums` directory.
3039

3140
### Setup your enum
3241

src/LaravelBackedEnumMakeCommand.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Webfox\LaravelBackedEnums;
44

5-
use Illuminate\Support\Str;
5+
use InvalidArgumentException;
66
use Illuminate\Console\GeneratorCommand;
77
use Symfony\Component\Console\Attribute\AsCommand;
88

@@ -53,8 +53,9 @@ protected function promptForMissingArgumentsUsing(): array
5353
'enumType' => [
5454
'What is the type of the enum?',
5555
match (strtolower($this->argument('enumType'))) {
56-
'int' => 'int',
57-
default => 'string',
56+
'int', 'integer' => 'int',
57+
'str', 'string' => 'string',
58+
default => throw new InvalidArgumentException('Enum type must be either "int" or "string"'),
5859
},
5960
],
6061
...parent::promptForMissingArgumentsUsing(),
@@ -63,12 +64,10 @@ protected function promptForMissingArgumentsUsing(): array
6364

6465
protected function getNameInput(): string
6566
{
66-
$name = trim($this->argument('name') . 'Enum');
67-
68-
if (Str::endsWith($name, '.php')) {
69-
return Str::substr($name, 0, -4);
67+
$name = trim($this->argument('name'));
68+
if (!preg_match('/^[A-Za-z_\x7f-\xff][A-Za-z0-9_\x7f-\xff]*$/', $name)) {
69+
throw new InvalidArgumentException('Invalid enum name format');
7070
}
71-
72-
return $name;
71+
return $name . 'Enum';
7372
}
7473
}

0 commit comments

Comments
 (0)