Skip to content

Commit 103369d

Browse files
authored
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGenerator-instead-of-Model
2 parents 05ce544 + 2570247 commit 103369d

File tree

12 files changed

+64
-39
lines changed

12 files changed

+64
-39
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
@@ -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: 21 additions & 4 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",
@@ -135,15 +136,15 @@ protected function getSearchValidationParameters(): array
135136
'per_page',
136137
]);
137138

138-
$parameters['array'] = ['with'];
139-
140139
$parameters['string'] = ['order_by'];
141140

142141
$parameters['string-nullable'] = ['query'];
143142

143+
$parameters['array'] = ['with'];
144+
144145
$parameters['string-required'] = ['with.*'];
145146

146-
return $this->getValidationParameters($parameters, false);
147+
return $this->getValidationParameters($parameters, true);
147148
}
148149

149150
public function getValidationParameters($parameters, $requiredAvailable): array
@@ -187,7 +188,7 @@ protected function getRules($name, $type, $required, $nullable, $present): array
187188
}
188189

189190
if ($required) {
190-
$rules[] = 'required';
191+
array_unshift($rules, 'required');
191192
}
192193

193194
if ($nullable) {
@@ -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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function rules(): array
1414

1515
return [
1616
'with' => 'array',
17-
'with.*' => 'string|required|in:' . $availableRelations,
17+
'with.*' => 'required|string|in:' . $availableRelations,
1818
];
1919
}
2020

@@ -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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ public function rules(): array
1717
'order_by' => 'string|in:' . $this->getOrderableFields(Post::class),
1818
'desc' => 'boolean',
1919
'all' => 'boolean',
20-
'with' => 'array',
2120
'query' => 'string|nullable',
22-
'with.*' => 'string|in:' . $availableRelations,
21+
'with' => 'array',
22+
'with.*' => 'required|string|in:' . $availableRelations,
2323
];
2424
}
2525

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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ public function rules(): array
1717
'order_by' => 'string|in:' . $this->getOrderableFields(Post::class),
1818
'desc' => 'boolean',
1919
'all' => 'boolean',
20-
'with' => 'array',
2120
'query' => 'string|nullable',
22-
'with.*' => 'string|in:' . $availableRelations,
21+
'with' => 'array',
22+
'with.*' => 'required|string|in:' . $availableRelations,
2323
];
2424
}
2525

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/create_request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CreatePostRequest extends Request
99
public function rules(): array
1010
{
1111
return [
12-
'user_id' => 'integer|exists:users,id|required',
12+
'user_id' => 'required|integer|exists:users,id',
1313
'is_draft' => 'boolean',
1414
'is_published' => 'boolean|present',
1515
];

tests/fixtures/RequestGeneratorTest/get_request.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function rules(): array
1414

1515
return [
1616
'with' => 'array',
17-
'with.*' => 'string|required|in:' . $availableRelations,
17+
'with.*' => 'required|string|in:' . $availableRelations,
1818
];
1919
}
2020

@@ -33,6 +33,8 @@ public function validateResolved(): void
3333
protected function getAvailableRelations(): array
3434
{
3535
return [
36+
'comments',
37+
'user',
3638
];
3739
}
3840
}

0 commit comments

Comments
 (0)