Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,17 @@ protected function pathToNamespace(string $name): string
{
return ucwords(Str::replace('/', '\\', $name), '\\');
}

protected function getRelationName(string $relation, string $type): string
{
$relationName = Str::snake($relation);

$pluralRelations = ['belongsToMany', 'hasMany'];

if (in_array($type, $pluralRelations)) {
$relationName = Str::plural($relationName);
}

return $relationName;
}
}
16 changes: 0 additions & 16 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

class ModelGenerator extends EntityGenerator
{
protected const array PLURAL_NUMBER_REQUIRED = [
'belongsToMany',
'hasMany',
];

public function generate(): void
{
if ($this->classExists('models', $this->model, $this->modelSubFolder)) {
Expand Down Expand Up @@ -154,17 +149,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 = [];
Expand Down
17 changes: 17 additions & 0 deletions src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected function createRequest($method, $needToValidate = true, $parameters =
'servicesNamespace' => $this->getNamespace('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",
Expand Down Expand Up @@ -206,6 +207,22 @@ protected function getRules($name, $type, $required, $nullable, $present): array
];
}

protected function getAvailableRelations(): array
{
$availableRelations = [];

$relations = $this->prepareRelations();

foreach ($relations as $type => $entities) {
$availableRelations = [
...$availableRelations,
...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)),
];
}

return $availableRelations;
}

private function getEntityName($method): string
{
if ($method === self::SEARCH_METHOD) {
Expand Down
9 changes: 9 additions & 0 deletions stubs/request.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ public function validateResolved(): void
@endif
@if($needToValidateWith)

@if(empty($availableRelations))
//TODO: don't forget to review relations list
@endif
protected function getAvailableRelations(): array
{
@if(!empty($availableRelations))
return [
@foreach($availableRelations as $relation)
'{{ $relation }}',
@endforeach
];
@else
return [];
@endif
}
@endif
}
1 change: 0 additions & 1 deletion tests/Support/Model/ModelMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ trait ModelMockTrait
{
use GeneratorMockTrait;


public function mockFileSystemWithoutCommentModel(): void
{
$fileSystemMock = new FileSystemMock();
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/CommandTest/get_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function validateResolved(): void
//TODO: don't forget to review relations list
protected function getAvailableRelations(): array
{
return [
];
return [];
}
}
3 changes: 1 addition & 2 deletions tests/fixtures/CommandTest/search_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function rules(): array
//TODO: don't forget to review relations list
protected function getAvailableRelations(): array
{
return [
];
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function rules(): array
//TODO: don't forget to review relations list
protected function getAvailableRelations(): array
{
return [
];
return [];
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/RequestGeneratorTest/get_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public function validateResolved(): void
}
}

//TODO: don't forget to review relations list
protected function getAvailableRelations(): array
{
return [
'comments',
'user',
];
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/RequestGeneratorTest/search_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public function rules(): array
];
}

//TODO: don't forget to review relations list
protected function getAvailableRelations(): array
{
return [
'comments',
'user',
];
}
}