Skip to content

Commit 2570247

Browse files
authored
Merge pull request #220 from RonasIT/217-available-relations
[217]: fill getAvailableRelations with passed relations
2 parents 508fa3d + 23da198 commit 2570247

File tree

9 files changed

+48
-23
lines changed

9 files changed

+48
-23
lines changed

src/Generators/EntityGenerator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,20 @@ protected function checkResourceExists(string $path, string $resourceName, ?stri
328328
throw new ResourceAlreadyExistsException($filePath);
329329
}
330330
}
331+
332+
protected function getRelationName(string $relation, string $type): string
333+
{
334+
$relationName = Str::snake($relation);
335+
336+
if ($this->isPluralRelation($type)) {
337+
$relationName = Str::plural($relationName);
338+
}
339+
340+
return $relationName;
341+
}
342+
343+
protected function isPluralRelation(string $relation): bool
344+
{
345+
return in_array($relation, ['hasMany', 'belongsToMany']);
346+
}
331347
}

src/Generators/ModelGenerator.php

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

1010
class ModelGenerator extends EntityGenerator
1111
{
12-
protected const array PLURAL_NUMBER_REQUIRED = [
13-
'belongsToMany',
14-
'hasMany',
15-
];
16-
1712
public function generate(): void
1813
{
1914
$this->checkResourceExists('models', $this->model, $this->modelSubFolder);
@@ -160,17 +155,6 @@ protected function getCasts(array $fields): array
160155
return $result;
161156
}
162157

163-
private function getRelationName(string $relation, string $type): string
164-
{
165-
$relationName = Str::snake($relation);
166-
167-
if (in_array($type, self::PLURAL_NUMBER_REQUIRED)) {
168-
$relationName = Str::plural($relationName);
169-
}
170-
171-
return $relationName;
172-
}
173-
174158
protected function getImportedRelations(): array
175159
{
176160
$result = [];
@@ -259,7 +243,7 @@ protected function isRequired(string $typeName): bool
259243

260244
protected function getRelationType(string $model, string $relation): string
261245
{
262-
if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) {
246+
if ($this->isPluralRelation($relation)) {
263247
return "Collection<{$model}>";
264248
}
265249

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)