diff --git a/composer.json b/composer.json index 3bbca44..1c83ae7 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/typescriptable-laravel", "description": "PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. If you want to use some helpers with Inertia, you can install associated NPM package.", - "version": "2.0.06", + "version": "2.0.07", "keywords": [ "kiwilan", "laravel", diff --git a/src/Typed/Eloquent/Output/EloquentPhp.php b/src/Typed/Eloquent/Output/EloquentPhp.php index 3467f72..aab224a 100644 --- a/src/Typed/Eloquent/Output/EloquentPhp.php +++ b/src/Typed/Eloquent/Output/EloquentPhp.php @@ -131,6 +131,8 @@ private function setField(EloquentProperty $property, bool $isLast): string $field .= PHP_EOL; } + $field = str_replace('\\\\', '\\', $field); + return $field; } diff --git a/src/Typed/Eloquent/Utils/EloquentCast.php b/src/Typed/Eloquent/Utils/EloquentCast.php index 2ce1881..5150a90 100644 --- a/src/Typed/Eloquent/Utils/EloquentCast.php +++ b/src/Typed/Eloquent/Utils/EloquentCast.php @@ -2,8 +2,10 @@ namespace Kiwilan\Typescriptable\Typed\Eloquent\Utils; +use BackedEnum; use Kiwilan\Typescriptable\Typed\Eloquent\EloquentItem; use ReflectionClass; +use UnitEnum; class EloquentCast { @@ -140,9 +142,15 @@ public static function enumsToTs(array $enum): string private function setEnums(ReflectionClass $reflector): array { $enums = []; - - foreach ($reflector->getConstants() as $name => $enum) { - $enums[$name] = is_string($enum) ? "'{$enum}'" : $enum->value; + $constants = $reflector->getConstants(); + $constants = array_filter($constants, fn ($value) => is_object($value)); + + foreach ($constants as $name => $enum) { + if ($enum instanceof BackedEnum) { + $enums[$name] = $enum->value; + } elseif ($enum instanceof UnitEnum) { + $enums[$name] = $enum->name; + } } return $enums; diff --git a/tests/Data/Enums/StoryFormatEnum.php b/tests/Data/Enums/StoryFormatEnum.php new file mode 100644 index 0000000..46ab840 --- /dev/null +++ b/tests/Data/Enums/StoryFormatEnum.php @@ -0,0 +1,21 @@ + 'datetime', 'status' => PublishStatusEnum::class, + 'format' => StoryFormatEnum::class, ]; /** diff --git a/tests/Data/database/migrations/create_models_tables.php b/tests/Data/database/migrations/create_models_tables.php index 237805d..10d7d55 100644 --- a/tests/Data/database/migrations/create_models_tables.php +++ b/tests/Data/database/migrations/create_models_tables.php @@ -33,6 +33,7 @@ public function up(): void $table->text('picture')->nullable(); $table->string('status')->default(PublishStatusEnum::draft->value); $table->dateTime('published_at')->nullable(); + $table->string('format')->nullable(); $table->string('meta_title')->nullable(); $table->string('meta_description')->nullable();