Skip to content
Merged
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: 7 additions & 6 deletions src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
Expand Down Expand Up @@ -287,12 +288,12 @@ protected function getCrudOptions()

protected function getRelations()
{
return [
'hasOne' => $this->option('has-one'),
'hasMany' => $this->option('has-many'),
'belongsTo' => $this->option('belongs-to'),
'belongsToMany' => $this->option('belongs-to-many')
];
return new RelationsDTO(
hasOne: $this->option('has-one'),
hasMany: $this->option('has-many'),
belongsTo: $this->option('belongs-to'),
belongsToMany: $this->option('belongs-to-many'),
);
}

protected function getFields()
Expand Down
19 changes: 19 additions & 0 deletions src/DTO/RelationsDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace RonasIT\Support\DTO;

readonly class RelationsDTO
{
public function __construct(
public array $hasOne = [],
public array $hasMany = [],
public array $belongsTo = [],
public array $belongsToMany = [],
) {
}

public function toArray(): array
{
return get_object_vars($this);
}
}
5 changes: 3 additions & 2 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
Expand Down Expand Up @@ -76,11 +77,11 @@ public function setFields($fields)
* @param array $relations
* @return $this
*/
public function setRelations($relations)
public function setRelations(RelationsDTO $relations)
{
$this->relations = $relations;

foreach ($relations['belongsTo'] as $field) {
foreach ($relations->belongsTo as $field) {
$name = Str::snake($field) . '_id';

$this->fields['integer-required'][] = $name;
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function generate(): void
'class' => $this->getPluralName($this->model),
'entity' => $this->model,
'entities' => $entities,
'relations' => $this->relations,
'relations' => $this->relations->toArray(),
'fields' => $this->fields,
'table' => $this->generateTable($this->fields)
]);
Expand Down
17 changes: 6 additions & 11 deletions src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@ class RequestsGenerator extends EntityGenerator
const DELETE_METHOD = 'Delete';
const GET_METHOD = 'Get';

public function setRelations($relations)
{
parent::setRelations($relations);

$this->relations['belongsTo'] = array_map(function ($field) {
return Str::snake($field) . '_id';
}, $this->relations['belongsTo']);

return $this;
}
protected array $relationFields = [];

public function generate(): void
{
if (!$this->isStubExists('request')) {
return;
}

$this->relationFields = array_map(function ($field) {
return Str::snake($field) . '_id';
}, $this->relations->belongsTo);

if (in_array('R', $this->crudOptions)) {
$this->createRequest(
self::GET_METHOD,
Expand Down Expand Up @@ -177,7 +172,7 @@ protected function getRules($name, $type, $required, $nullable, $present): array
Arr::get($replaces, $type, $type)
];

if (in_array($name, $this->relations['belongsTo'])) {
if (in_array($name, $this->relationFields)) {
$tableName = str_replace('_id', '', $name);

$rules[] = "exists:{$this->getTableName($tableName)},id";
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function createEntitySeeder(): void
{
$content = "<?php \n\n" . $this->getStub('seeder', [
'entity' => $this->model,
'relations' => $this->relations,
'relations' => $this->relations->toArray(),
'namespace' => $this->getOrCreateNamespace('seeders'),
'factoryNamespace' => $this->getOrCreateNamespace('factories'),
]);
Expand Down
12 changes: 1 addition & 11 deletions src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;

class ServiceGenerator extends EntityGenerator
{
public function setRelations($relations)
{
foreach ($relations['belongsTo'] as $field) {
$name = Str::snake($field) . '_id';

$this->fields['integer'][] = $name;
}

return $this;
}

public function generate(): void
{
if ($this->classExists('repositories', "{$this->model}Repository")) {
Expand Down
43 changes: 18 additions & 25 deletions tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\View\ViewException;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
Expand Down Expand Up @@ -65,11 +66,7 @@ className: ViewException::class,
->setFields([
'another_type' => ['some_field'],
])
->setRelations([
'hasOne' => [],
'hasMany' => [],
'belongsTo' => [],
])
->setRelations(new RelationsDTO())
->setModel('Post')
->generate();
}
Expand All @@ -84,11 +81,10 @@ public function testCreateSuccess()
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setRelations(new RelationsDTO(
hasOne: ['user'],
belongsTo: ['user'],
))
->setModel('Post')
->generate();

Expand All @@ -112,11 +108,10 @@ public function testCreateFactoryWithoutFactoryStub(): void
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setRelations(new RelationsDTO(
hasOne: ['user'],
belongsTo: ['user'],
))
->setModel('Post')
->generate();

Expand Down Expand Up @@ -144,11 +139,10 @@ public function testConfigFolderWithIncorrectCase(): void
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setRelations(new RelationsDTO(
hasOne: ['user'],
belongsTo: ['user'],
))
->setModel('Post')
->generate();
}
Expand All @@ -165,11 +159,10 @@ public function testConfigFolderWithExtension(): void
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setRelations(new RelationsDTO(
hasOne: ['user'],
belongsTo: ['user'],
))
->setModel('Post')
->generate();

Expand Down
29 changes: 5 additions & 24 deletions tests/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RonasIT\Support\Tests;

use Illuminate\Support\Carbon;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
use RonasIT\Support\Generators\MigrationGenerator;
Expand All @@ -18,12 +19,7 @@ className: UnknownFieldTypeException::class,

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'unknown-type' => ['title'],
Expand All @@ -37,12 +33,7 @@ public function testCreateMigration()

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'string' => ['title', 'body'],
Expand All @@ -62,12 +53,7 @@ public function testCreateMigrationMYSQL()

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'string' => ['title', 'body'],
Expand All @@ -88,12 +74,7 @@ public function testCreateMigrationWithoutMigrationStub(): void

app(MigrationGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => [],
'belongsToMany' => [],
'hasOne' => [],
'hasMany' => [],
])
->setRelations(new RelationsDTO())
->setFields([
'integer-required' => ['media_id', 'user_id'],
'string' => ['title', 'body'],
Expand Down
31 changes: 13 additions & 18 deletions tests/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
Expand Down Expand Up @@ -39,12 +40,9 @@ className: ClassNotExistsException::class,

app(ModelGenerator::class)
->setModel('Post')
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => [],
'belongsTo' => [],
'belongsToMany' => [],
])
->setRelations(new RelationsDTO(
hasOne: ['Comment']
))
->generate();
}

Expand All @@ -58,12 +56,10 @@ public function testCreateModel()
'integer-required' => ['media_id'],
'boolean-required' => ['is_published'],
])
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => ['User'],
'belongsTo' => [],
'belongsToMany' => [],
])
->setRelations(new RelationsDTO(
hasOne: ['Comment'],
hasMany: ['User'],
))
->generate();

$this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php');
Expand Down Expand Up @@ -105,6 +101,7 @@ public function testCreateModelWithoutRelationsRelationStubNotExist()

app(ModelGenerator::class)
->setModel('Post')
->setRelations(new RelationsDTO())
->setFields([])
->generate();

Expand All @@ -125,12 +122,10 @@ public function testCreateModelWithRelationsRelationStubNotExist()
app(ModelGenerator::class)
->setModel('Post')
->setFields([])
->setRelations([
'hasOne' => ['Comment'],
'hasMany' => ['User'],
'belongsTo' => [],
'belongsToMany' => [],
])
->setRelations(new RelationsDTO(
hasOne: ['Comment'],
hasMany: ['User'],
))
->generate();

$this->assertFileDoesNotExist('new_model.php', 'app/Models/Post.php');
Expand Down
11 changes: 5 additions & 6 deletions tests/RequestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Generators\RequestsGenerator;
Expand All @@ -12,12 +13,10 @@ public function testCreateRequests()
{
app(RequestsGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => ['User'],
'hasMany' => ['Comments'],
'hasOne' => [],
'belongsToMany' => []
])
->setRelations(new RelationsDTO(
hasMany: ['Comments'],
belongsTo: ['User'],
))
->setFields([
'boolean-required' => ['is_published'],
'integer' => ['user_id'],
Expand Down
Loading