Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function handle(): void
$this->checkConfigs();
$this->listenEvents();
$this->parseRelations();
$this->entityName = $this->convertToPascalCase($this->entityName);

try {
$this->generate();
Expand Down Expand Up @@ -238,19 +239,20 @@ protected function getCrudOptions(): array
protected function parseRelations(): void
{
$this->relations = new RelationsDTO(
hasOne: $this->trimRelations($this->option('has-one')),
hasMany: $this->trimRelations($this->option('has-many')),
belongsTo: $this->trimRelations($this->option('belongs-to')),
belongsToMany: $this->trimRelations($this->option('belongs-to-many')),
hasOne: $this->prepareRelations($this->option('has-one')),
hasMany: $this->prepareRelations($this->option('has-many')),
belongsTo: $this->prepareRelations($this->option('belongs-to')),
belongsToMany: $this->prepareRelations($this->option('belongs-to-many')),
);
}

protected function trimRelations(array $relations): array
protected function prepareRelations(array $relations): array
{
return array_map(
callback: fn ($relation) => Str::trim($relation, '/'),
array: $relations,
);
return array_map(function ($relation) {
$relation = Str::trim($relation, '/');

return $this->convertToPascalCase($relation);
}, $relations);
}

protected function getFields(): array
Expand Down Expand Up @@ -305,4 +307,15 @@ protected function listenEvents(): void
listener: fn (WarningEvent $event) => $this->warn($event->message),
);
}

protected function convertToPascalCase(string $entityName): string
{
$pascalEntityName = Str::studly($entityName);

if ($entityName !== $pascalEntityName) {
$this->info("{$entityName} was converted to pascal case {$pascalEntityName}");
}

return $pascalEntityName;
}
}
2 changes: 1 addition & 1 deletion src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function setCrudOptions(array $crudOptions): self

public function setModel(string $model): self
{
$this->model = Str::studly($model);
$this->model = $model;

return $this;
}
Expand Down
5 changes: 4 additions & 1 deletion tests/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ className: SuccessCreateMessage::class,
public function testCreateModelHasMultipleRelationsWithAnotherModel()
{
$this
->artisan('make:entity Forum/Post -A User -E User --only-model')
->artisan('make:entity Forum/post -A user -E /user --only-model')
->expectsOutput('user was converted to pascal case User')
->expectsOutput('user was converted to pascal case User')
->expectsOutput('post was converted to pascal case Post')
->assertSuccessful();

$this->assertGeneratedFileEquals('new_model_with_many_relations.php', 'app/Models/Forum/Post.php');
Expand Down