Skip to content

Commit c6caf86

Browse files
authored
Merge branch 'master' into add-resource-not-exists-exception
2 parents 76dd206 + 2570247 commit c6caf86

File tree

10 files changed

+49
-24
lines changed

10 files changed

+49
-24
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ protected function convertToPascalCase(string $entityName): string
313313
$pascalEntityName = Str::studly($entityName);
314314

315315
if ($entityName !== $pascalEntityName) {
316-
$this->info("{$entityName} was converted to {$pascalEntityName}");
316+
$this->warn("{$entityName} was converted to {$pascalEntityName}");
317317
}
318318

319319
return $pascalEntityName;

src/Generators/EntityGenerator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,20 @@ protected function checkResourceNotExists(string $path, string $entity, string $
338338
throw new ResourceNotExistsException($entity, $filePath);
339339
}
340340
}
341+
342+
protected function getRelationName(string $relation, string $type): string
343+
{
344+
$relationName = Str::snake($relation);
345+
346+
if ($this->isPluralRelation($type)) {
347+
$relationName = Str::plural($relationName);
348+
}
349+
350+
return $relationName;
351+
}
352+
353+
protected function isPluralRelation(string $relation): bool
354+
{
355+
return in_array($relation, ['hasMany', 'belongsToMany']);
356+
}
341357
}

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88

99
class ModelGenerator extends EntityGenerator
1010
{
11-
protected const array PLURAL_NUMBER_REQUIRED = [
12-
'belongsToMany',
13-
'hasMany',
14-
];
15-
1611
public function generate(): void
1712
{
1813
$this->checkResourceExists('models', $this->model, $this->modelSubFolder);
@@ -152,17 +147,6 @@ protected function getCasts(array $fields): array
152147
return $result;
153148
}
154149

155-
private function getRelationName(string $relation, string $type): string
156-
{
157-
$relationName = Str::snake($relation);
158-
159-
if (in_array($type, self::PLURAL_NUMBER_REQUIRED)) {
160-
$relationName = Str::plural($relationName);
161-
}
162-
163-
return $relationName;
164-
}
165-
166150
protected function getImportedRelations(): array
167151
{
168152
$result = [];
@@ -251,7 +235,7 @@ protected function isRequired(string $typeName): bool
251235

252236
protected function getRelationType(string $model, string $relation): string
253237
{
254-
if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) {
238+
if ($this->isPluralRelation($relation)) {
255239
return "Collection<{$model}>";
256240
}
257241

src/Generators/RequestsGenerator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected function createRequest($method, $needToValidate = true, $parameters =
7979
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
8080
'entityNamespace' => $this->getModelClass($this->model),
8181
'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')),
82+
'availableRelations' => $this->getAvailableRelations(),
8283
]);
8384

8485
$this->saveClass('requests', "{$method}{$modelName}Request",
@@ -208,6 +209,22 @@ protected function getRules($name, $type, $required, $nullable, $present): array
208209
];
209210
}
210211

212+
protected function getAvailableRelations(): array
213+
{
214+
$availableRelations = [];
215+
216+
$relations = $this->prepareRelations();
217+
218+
foreach ($relations as $type => $entities) {
219+
array_push(
220+
$availableRelations,
221+
...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)),
222+
);
223+
}
224+
225+
return $availableRelations;
226+
}
227+
211228
private function getEntityName($method): string
212229
{
213230
if ($method === self::SEARCH_METHOD) {

stubs/request.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ public function validateResolved(): void
5050
//TODO: don't forget to review relations list
5151
protected function getAvailableRelations(): array
5252
{
53+
@if(!empty($availableRelations))
5354
return [
55+
@foreach($availableRelations as $relation)
56+
'{{ $relation }}',
57+
@endforeach
5458
];
59+
@else
60+
return [];
61+
@endif
5562
}
5663
@endif
5764
}

tests/fixtures/CommandTest/get_request.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function validateResolved(): void
3232
//TODO: don't forget to review relations list
3333
protected function getAvailableRelations(): array
3434
{
35-
return [
36-
];
35+
return [];
3736
}
3837
}

tests/fixtures/CommandTest/search_request.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public function rules(): array
2626
//TODO: don't forget to review relations list
2727
protected function getAvailableRelations(): array
2828
{
29-
return [
30-
];
29+
return [];
3130
}
3231
}

tests/fixtures/CommandTest/search_request_subfolder_model.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public function rules(): array
2626
//TODO: don't forget to review relations list
2727
protected function getAvailableRelations(): array
2828
{
29-
return [
30-
];
29+
return [];
3130
}
3231
}

tests/fixtures/RequestGeneratorTest/get_request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function validateResolved(): void
3333
protected function getAvailableRelations(): array
3434
{
3535
return [
36+
'comments',
37+
'user',
3638
];
3739
}
3840
}

tests/fixtures/RequestGeneratorTest/search_request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function rules(): array
2929
protected function getAvailableRelations(): array
3030
{
3131
return [
32+
'comments',
33+
'user',
3234
];
3335
}
3436
}

0 commit comments

Comments
 (0)