diff --git a/src/Console/Commands/GenerateCommand.php b/src/Console/Commands/GenerateCommand.php index dea6af9..2437b69 100644 --- a/src/Console/Commands/GenerateCommand.php +++ b/src/Console/Commands/GenerateCommand.php @@ -77,12 +77,17 @@ protected function writeFile(string $class) $reflection = new \ReflectionClass($class); + $is_enum = method_exists($reflection, 'isEnum') && $reflection->isEnum(); + $outputString = ''; foreach ($reflection->getConstants() as $key => $value) { - if (gettype($value) == gettype(" ") ) { - $outputString .= sprintf("export const %s = \"%s\"\n", $key, $value); + if ($is_enum) { + $value = property_exists($value, 'value') ? $value->value : $value->name; } - else { + + if (gettype($value) == gettype(" ")) { + $outputString .= sprintf("export const %s = \"%s\"\n", $key, $value); + } else { $outputString .= sprintf("export const %s = %s\n", $key, $value); } } diff --git a/tests/Console/Commands/GenerateCommandTest.php b/tests/Console/Commands/GenerateCommandTest.php index f98b9e3..f5dec7e 100644 --- a/tests/Console/Commands/GenerateCommandTest.php +++ b/tests/Console/Commands/GenerateCommandTest.php @@ -22,12 +22,18 @@ public function testGenerate() // Include a couple of classes for this test. include('tests/resources/Enums/Status.php'); include('tests/resources/Enums/Sub/Type.php'); + include('tests/resources/Enums/Native/BackedString.php'); + include('tests/resources/Enums/Native/Base.php'); + include('tests/resources/Enums/Native/BackedInt.php'); Artisan::call('enum-js:generate'); $generatedFiles = Storage::disk(config('laravel-enum-js.output_disk'))->allFiles(); $this->assertSame([ + 'Native/BackedInt.js', + 'Native/BackedString.js', + 'Native/Base.js', 'Status.js', 'Sub/Type.js', ], $generatedFiles); diff --git a/tests/resources/Enums/Native/BackedInt.php b/tests/resources/Enums/Native/BackedInt.php new file mode 100644 index 0000000..21f164f --- /dev/null +++ b/tests/resources/Enums/Native/BackedInt.php @@ -0,0 +1,9 @@ +