Skip to content

Commit

Permalink
Merge pull request #80 from kiwilan/develop
Browse files Browse the repository at this point in the history
v3.1.02
  • Loading branch information
ewilan-riviere authored Jul 25, 2024
2 parents a833ea2 + 13f579d commit 0776a42
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 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": "3.1.01",
"version": "3.1.02",
"keywords": [
"kiwilan",
"laravel",
Expand Down
22 changes: 21 additions & 1 deletion src/Typed/Eloquent/Schemas/Model/SchemaModelRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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('/(?<!^)[A-Z]/', '_$0', $string);
$string = strtolower($string);

return $string;
}

private function relationTypeisMany(string $type): bool
{
if (in_array($type, [
Expand Down
2 changes: 1 addition & 1 deletion src/Typed/Eloquent/Schemas/SchemaApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function improveRelations(array $models): array

if ($relation->isMany()) {
$model->setAttribute(new SchemaModelAttribute(
name: $relation->name().'_count',
name: $relation->snakeCaseName().'_count',
databaseType: null,
increments: false,
nullable: true,
Expand Down
19 changes: 1 addition & 18 deletions src/Typed/Utils/Schema/SchemaClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/EloquentListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion tests/SchemaAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_');
Expand Down
2 changes: 1 addition & 1 deletion tests/SchemaCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 0776a42

Please sign in to comment.