Skip to content

Commit

Permalink
v2.0.07
Browse files Browse the repository at this point in the history
- Fix `EloquentPhp::class` for `\` duplicates
- Fix `EloquentCast::class` with `UnitEnum` and public constants into enum classes
  • Loading branch information
ewilan-riviere committed Jun 14, 2024
1 parent 743939a commit e95af7c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/Typed/Eloquent/Output/EloquentPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ private function setField(EloquentProperty $property, bool $isLast): string
$field .= PHP_EOL;
}

$field = str_replace('\\\\', '\\', $field);

return $field;
}

Expand Down
14 changes: 11 additions & 3 deletions src/Typed/Eloquent/Utils/EloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kiwilan\Typescriptable\Typed\Eloquent\Utils;

use BackedEnum;
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentItem;
use ReflectionClass;
use UnitEnum;

class EloquentCast
{
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/Data/Enums/StoryFormatEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Kiwilan\Typescriptable\Tests\Data\Enums;

/**
* List of available formats.
*/
enum StoryFormatEnum
{
public const ALLOWED_EXTENSIONS = ['mp3', 'm4b', 'pdf', 'cb7', 'cba', 'cbr', 'cbt', 'cbz', 'epub'];

case unknown;

case audio;

case pdf;

case cba;

case epub;
}
3 changes: 3 additions & 0 deletions tests/Data/Models/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Kiwilan\Typescriptable\Tests\Data\Enums\PublishStatusEnum;
use Kiwilan\Typescriptable\Tests\Data\Enums\StoryFormatEnum;

class Story extends Model
{
Expand All @@ -22,6 +23,7 @@ class Story extends Model
'picture',
'published_at',
'status',
'format',
];

protected $appends = [
Expand All @@ -35,6 +37,7 @@ class Story extends Model
protected $casts = [
'published_at' => 'datetime',
'status' => PublishStatusEnum::class,
'format' => StoryFormatEnum::class,
];

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Data/database/migrations/create_models_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e95af7c

Please sign in to comment.