Skip to content

Commit

Permalink
Merge pull request #8 from bentleyo/patch/enum-constants
Browse files Browse the repository at this point in the history
Add support for constants inside an enum
  • Loading branch information
modstore authored Aug 23, 2024
2 parents cda2478 + 68b3b12 commit f690121
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/Console/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ 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 ($is_enum) {
foreach ($reflection->getReflectionConstants() as $constant) {
$value = $constant->getValue();
if (method_exists($constant, 'isEnumCase') && $constant->isEnumCase()) {
$value = property_exists($value, 'value') ? $value->value : $value->name;
}

$outputString .= sprintf("export const %s = %s\n", $key, json_encode($value));
$outputString .= sprintf("export const %s = %s\n", $constant->getName(), json_encode($value));
}

Storage::disk(config('laravel-enum-js.output_disk'))->put($outputPath, $outputString);
Expand Down
6 changes: 3 additions & 3 deletions tests/Console/Commands/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public function generatedContentDataProvider(): array
],
'native' => [
'filename' => 'Native/Base.php',
'expectedContent' => "export const Value1 = \"Value1\"\nexport const Value2 = \"Value2\"\n",
'expectedContent' => "export const Value1 = \"Value1\"\nexport const Value2 = \"Value2\"\nexport const ADDITIONAL_CONST = [\"example\"]\n",
],
'native backed int' => [
'filename' => 'Native/BackedInt.php',
'expectedContent' => "export const Value1 = 1\nexport const Value2 = 2\n",
'expectedContent' => "export const Value1 = 1\nexport const Value2 = 2\nexport const ADDITIONAL_CONST = [\"example\"]\n",
],
'native backed string' => [
'filename' => 'Native/BackedString.php',
'expectedContent' => "export const Value1 = \"value-1\"\nexport const Value2 = \"value-2\"\n",
'expectedContent' => "export const Value1 = \"value-1\"\nexport const Value2 = \"value-2\"\nexport const ADDITIONAL_CONST = [\"example\"]\n",
],
'array' => [
'filename' => 'ArrayValue.php',
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/Enums/Native/BackedInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ enum BackedInt : int
{
case Value1 = 1;
case Value2 = 2;

const ADDITIONAL_CONST = [
'example',
];
}
4 changes: 4 additions & 0 deletions tests/resources/Enums/Native/BackedString.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ enum BackedString : string
{
case Value1 = 'value-1';
case Value2 = 'value-2';

const ADDITIONAL_CONST = [
'example',
];
}
4 changes: 4 additions & 0 deletions tests/resources/Enums/Native/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ enum Base
{
case Value1;
case Value2;

const ADDITIONAL_CONST = [
'example',
];
}

0 comments on commit f690121

Please sign in to comment.