diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index 764f1b5..ce3cbc8 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -328,4 +328,20 @@ protected function checkResourceExists(string $path, string $resourceName, ?stri throw new ResourceAlreadyExistsException($filePath); } } + + protected function getRelationName(string $relation, string $type): string + { + $relationName = Str::snake($relation); + + if ($this->isPluralRelation($type)) { + $relationName = Str::plural($relationName); + } + + return $relationName; + } + + protected function isPluralRelation(string $relation): bool + { + return in_array($relation, ['hasMany', 'belongsToMany']); + } } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 4ca0329..6a11e80 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -9,11 +9,6 @@ class ModelGenerator extends EntityGenerator { - protected const array PLURAL_NUMBER_REQUIRED = [ - 'belongsToMany', - 'hasMany', - ]; - public function generate(): void { $this->checkResourceExists('models', $this->model, $this->modelSubFolder); @@ -160,17 +155,6 @@ protected function getCasts(array $fields): array return $result; } - private function getRelationName(string $relation, string $type): string - { - $relationName = Str::snake($relation); - - if (in_array($type, self::PLURAL_NUMBER_REQUIRED)) { - $relationName = Str::plural($relationName); - } - - return $relationName; - } - protected function getImportedRelations(): array { $result = []; @@ -259,7 +243,7 @@ protected function isRequired(string $typeName): bool protected function getRelationType(string $model, string $relation): string { - if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) { + if ($this->isPluralRelation($relation)) { return "Collection<{$model}>"; } diff --git a/src/Generators/RequestsGenerator.php b/src/Generators/RequestsGenerator.php index 3bc939b..52b09ac 100644 --- a/src/Generators/RequestsGenerator.php +++ b/src/Generators/RequestsGenerator.php @@ -79,6 +79,7 @@ protected function createRequest($method, $needToValidate = true, $parameters = 'servicesNamespace' => $this->generateNamespace($this->paths['services']), 'entityNamespace' => $this->getModelClass($this->model), 'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')), + 'availableRelations' => $this->getAvailableRelations(), ]); $this->saveClass('requests', "{$method}{$modelName}Request", @@ -208,6 +209,22 @@ protected function getRules($name, $type, $required, $nullable, $present): array ]; } + protected function getAvailableRelations(): array + { + $availableRelations = []; + + $relations = $this->prepareRelations(); + + foreach ($relations as $type => $entities) { + array_push( + $availableRelations, + ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)), + ); + } + + return $availableRelations; + } + private function getEntityName($method): string { if ($method === self::SEARCH_METHOD) { diff --git a/stubs/request.blade.php b/stubs/request.blade.php index bde5819..24b0ebc 100644 --- a/stubs/request.blade.php +++ b/stubs/request.blade.php @@ -50,8 +50,15 @@ public function validateResolved(): void //TODO: don't forget to review relations list protected function getAvailableRelations(): array { +@if(!empty($availableRelations)) return [ +@foreach($availableRelations as $relation) + '{{ $relation }}', +@endforeach ]; +@else + return []; +@endif } @endif } \ No newline at end of file diff --git a/tests/fixtures/CommandTest/get_request.php b/tests/fixtures/CommandTest/get_request.php index f3baadf..84cb9bb 100644 --- a/tests/fixtures/CommandTest/get_request.php +++ b/tests/fixtures/CommandTest/get_request.php @@ -32,7 +32,6 @@ public function validateResolved(): void //TODO: don't forget to review relations list protected function getAvailableRelations(): array { - return [ - ]; + return []; } } \ No newline at end of file diff --git a/tests/fixtures/CommandTest/search_request.php b/tests/fixtures/CommandTest/search_request.php index fdfb22b..5fe8838 100644 --- a/tests/fixtures/CommandTest/search_request.php +++ b/tests/fixtures/CommandTest/search_request.php @@ -26,7 +26,6 @@ public function rules(): array //TODO: don't forget to review relations list protected function getAvailableRelations(): array { - return [ - ]; + return []; } } \ No newline at end of file diff --git a/tests/fixtures/CommandTest/search_request_subfolder_model.php b/tests/fixtures/CommandTest/search_request_subfolder_model.php index 3f86452..e4ea4a1 100644 --- a/tests/fixtures/CommandTest/search_request_subfolder_model.php +++ b/tests/fixtures/CommandTest/search_request_subfolder_model.php @@ -26,7 +26,6 @@ public function rules(): array //TODO: don't forget to review relations list protected function getAvailableRelations(): array { - return [ - ]; + return []; } } \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/get_request.php b/tests/fixtures/RequestGeneratorTest/get_request.php index f3baadf..eef2217 100644 --- a/tests/fixtures/RequestGeneratorTest/get_request.php +++ b/tests/fixtures/RequestGeneratorTest/get_request.php @@ -33,6 +33,8 @@ public function validateResolved(): void protected function getAvailableRelations(): array { return [ + 'comments', + 'user', ]; } } \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/search_request.php b/tests/fixtures/RequestGeneratorTest/search_request.php index ad7b837..31a907e 100644 --- a/tests/fixtures/RequestGeneratorTest/search_request.php +++ b/tests/fixtures/RequestGeneratorTest/search_request.php @@ -29,6 +29,8 @@ public function rules(): array protected function getAvailableRelations(): array { return [ + 'comments', + 'user', ]; } } \ No newline at end of file