diff --git a/composer.json b/composer.json index e1116ae..ef78c33 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": "3.1.01", + "version": "3.1.02", "keywords": [ "kiwilan", "laravel", diff --git a/src/Typed/Eloquent/Schemas/Model/SchemaModelRelation.php b/src/Typed/Eloquent/Schemas/Model/SchemaModelRelation.php index 01cf3ba..310bee0 100644 --- a/src/Typed/Eloquent/Schemas/Model/SchemaModelRelation.php +++ b/src/Typed/Eloquent/Schemas/Model/SchemaModelRelation.php @@ -8,6 +8,7 @@ protected function __construct( protected string $name, protected ?string $laravelType = null, protected ?string $relatedToModel = null, + protected ?string $snakeCaseName = null, protected bool $isInternal = true, protected bool $isMany = false, protected string $phpType = 'mixed', @@ -21,6 +22,7 @@ public static function make(array $data): self $data['type'] ?? null, $data['related'] ?? null, ); + $self->snakeCaseName = $self->toSnakeCaseName($self->name); $self->isMany = $self->relationTypeisMany($self->laravelType); $self->phpType = $self->relatedToModel; if ($self->isMany) { @@ -45,6 +47,11 @@ public function relatedToModel(): ?string return $this->relatedToModel; } + public function snakeCaseName(): string + { + return $this->snakeCaseName; + } + public function isInternal(): bool { return $this->isInternal; @@ -83,7 +90,11 @@ public function setTypescriptType(string $typescriptType, string $baseNamespace) } if ($this->isInternal) { - $this->typescriptType = "App.Models.{$typescriptType}"; + if ($typescriptType === 'any') { + $this->typescriptType = 'any'; + } else { + $this->typescriptType = "App.Models.{$typescriptType}"; + } } else { $this->typescriptType = $typescriptType; } @@ -95,6 +106,15 @@ public function setTypescriptType(string $typescriptType, string $baseNamespace) return $this; } + private function toSnakeCaseName(string $string): string + { + $string = preg_replace('/\s+/', '', $string); + $string = preg_replace('/(?isMany()) { $model->setAttribute(new SchemaModelAttribute( - name: $relation->name().'_count', + name: $relation->snakeCaseName().'_count', databaseType: null, increments: false, nullable: true, diff --git a/src/Typed/Utils/Schema/SchemaClass.php b/src/Typed/Utils/Schema/SchemaClass.php index 6a1677e..a63a2aa 100644 --- a/src/Typed/Utils/Schema/SchemaClass.php +++ b/src/Typed/Utils/Schema/SchemaClass.php @@ -59,7 +59,7 @@ traits: $reflect->getTraitNames(), extends: $parent ? $parent->getName() : null, ); - if ($parser->extends === 'Illuminate\Database\Eloquent\Model') { + if ($parser->extends === 'Illuminate\Database\Eloquent\Model' || $parser->extends === 'Illuminate\Foundation\Auth\User') { $parser->isModel = true; } @@ -128,23 +128,6 @@ public function extends(): ?string return $this->extends; } - // /** - // * @param ClassProperty[] $properties - // */ - // public static function fake(string $name, array $properties): self - // { - // $snake = Str::snake($name); - // $table = Str::plural($snake); - - // $self = new self( - // table: $table, - // name: $name, - // ); - // $class->typeableModel = EloquentModel::fake($class, $properties); - - // return $self; - // } - private static function fileNamespace(SplFileInfo $file): string { $path = $file->getPathName(); diff --git a/tests/EloquentListTest.php b/tests/EloquentListTest.php index c624b70..fcd517c 100644 --- a/tests/EloquentListTest.php +++ b/tests/EloquentListTest.php @@ -12,7 +12,7 @@ expect($list->models())->toBeArray(); expect($list->path())->toBe(models()); - expect(count($list->models()))->toBe(8); + expect(count($list->models()))->toBe(9); }); it('can use command', function () { diff --git a/tests/SchemaAppTest.php b/tests/SchemaAppTest.php index f47f0a7..4c3dffc 100644 --- a/tests/SchemaAppTest.php +++ b/tests/SchemaAppTest.php @@ -18,7 +18,7 @@ expect($app->useParser())->toBeFalse(); expect($app->baseNamespace())->toBe('Kiwilan\Typescriptable\Tests\Data\Models'); expect($app->models())->toBeArray(); - expect(count($app->models()))->toBe(8); + expect(count($app->models()))->toBe(9); expect($app->driver())->toBe($driver); expect($app->databaseName())->toBeIn(['testing', ':memory:']); expect($app->databasePrefix())->toBe('ts_'); diff --git a/tests/SchemaCollectionTest.php b/tests/SchemaCollectionTest.php index 6eee251..bcd02c3 100644 --- a/tests/SchemaCollectionTest.php +++ b/tests/SchemaCollectionTest.php @@ -7,7 +7,7 @@ $schemas = $collect->onlyModels(); expect($schemas)->toBeArray(); - expect(count($schemas))->toBe(9); + expect(count($schemas))->toBe(10); $movie = $schemas['Movie']; expect($movie->basePath())->toContain('tests/Data/Models');