Skip to content

Commit bd9e489

Browse files
authored
Merge pull request #218 from RonasIT/216-entities-name
[216] convert entity and relation names to PascalCase
2 parents 6e3ec23 + e155076 commit bd9e489

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function handle(): void
124124
$this->checkConfigs();
125125
$this->listenEvents();
126126
$this->parseRelations();
127+
$this->entityName = $this->convertToPascalCase($this->entityName);
127128

128129
try {
129130
$this->generate();
@@ -238,19 +239,20 @@ protected function getCrudOptions(): array
238239
protected function parseRelations(): void
239240
{
240241
$this->relations = new RelationsDTO(
241-
hasOne: $this->trimRelations($this->option('has-one')),
242-
hasMany: $this->trimRelations($this->option('has-many')),
243-
belongsTo: $this->trimRelations($this->option('belongs-to')),
244-
belongsToMany: $this->trimRelations($this->option('belongs-to-many')),
242+
hasOne: $this->prepareRelations($this->option('has-one')),
243+
hasMany: $this->prepareRelations($this->option('has-many')),
244+
belongsTo: $this->prepareRelations($this->option('belongs-to')),
245+
belongsToMany: $this->prepareRelations($this->option('belongs-to-many')),
245246
);
246247
}
247248

248-
protected function trimRelations(array $relations): array
249+
protected function prepareRelations(array $relations): array
249250
{
250-
return array_map(
251-
callback: fn ($relation) => Str::trim($relation, '/'),
252-
array: $relations,
253-
);
251+
return array_map(function ($relation) {
252+
$relation = Str::trim($relation, '/');
253+
254+
return $this->convertToPascalCase($relation);
255+
}, $relations);
254256
}
255257

256258
protected function getFields(): array
@@ -305,4 +307,15 @@ protected function listenEvents(): void
305307
listener: fn (WarningEvent $event) => $this->warn($event->message),
306308
);
307309
}
310+
311+
protected function convertToPascalCase(string $entityName): string
312+
{
313+
$pascalEntityName = Str::studly($entityName);
314+
315+
if ($entityName !== $pascalEntityName) {
316+
$this->info("{$entityName} was converted to {$pascalEntityName}");
317+
}
318+
319+
return $pascalEntityName;
320+
}
308321
}

src/Generators/EntityGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function setCrudOptions(array $crudOptions): self
5353

5454
public function setModel(string $model): self
5555
{
56-
$this->model = Str::studly($model);
56+
$this->model = $model;
5757

5858
return $this;
5959
}

tests/ModelGeneratorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ className: SuccessCreateMessage::class,
153153
public function testCreateModelHasMultipleRelationsWithAnotherModel()
154154
{
155155
$this
156-
->artisan('make:entity Forum/Post -A User -E User --only-model')
156+
->artisan('make:entity Forum/post -A user -E /user --only-model')
157+
->expectsOutput('user was converted to User')
158+
->expectsOutput('user was converted to User')
159+
->expectsOutput('post was converted to Post')
157160
->assertSuccessful();
158161

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

0 commit comments

Comments
 (0)