Skip to content

Commit 70673ab

Browse files
authored
Merge pull request #151 from RonasIT/150-implement-relations-dto
150 implement relations dto
2 parents 6bd833d + 40aa3c9 commit 70673ab

13 files changed

+102
-135
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Facades\Config;
88
use Illuminate\Support\Str;
9+
use RonasIT\Support\DTO\RelationsDTO;
910
use RonasIT\Support\Events\SuccessCreateMessage;
1011
use RonasIT\Support\Events\WarningEvent;
1112
use RonasIT\Support\Exceptions\ClassNotExistsException;
@@ -287,12 +288,12 @@ protected function getCrudOptions()
287288

288289
protected function getRelations()
289290
{
290-
return [
291-
'hasOne' => $this->option('has-one'),
292-
'hasMany' => $this->option('has-many'),
293-
'belongsTo' => $this->option('belongs-to'),
294-
'belongsToMany' => $this->option('belongs-to-many')
295-
];
291+
return new RelationsDTO(
292+
hasOne: $this->option('has-one'),
293+
hasMany: $this->option('has-many'),
294+
belongsTo: $this->option('belongs-to'),
295+
belongsToMany: $this->option('belongs-to-many'),
296+
);
296297
}
297298

298299
protected function getFields()

src/DTO/RelationsDTO.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace RonasIT\Support\DTO;
4+
5+
readonly class RelationsDTO
6+
{
7+
public function __construct(
8+
public array $hasOne = [],
9+
public array $hasMany = [],
10+
public array $belongsTo = [],
11+
public array $belongsToMany = [],
12+
) {
13+
}
14+
15+
public function toArray(): array
16+
{
17+
return get_object_vars($this);
18+
}
19+
}

src/Generators/EntityGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Facades\DB;
99
use Illuminate\Support\Str;
10+
use RonasIT\Support\DTO\RelationsDTO;
1011
use RonasIT\Support\Events\WarningEvent;
1112
use RonasIT\Support\Exceptions\ClassNotExistsException;
1213
use RonasIT\Support\Exceptions\IncorrectClassPathException;
@@ -76,11 +77,11 @@ public function setFields($fields)
7677
* @param array $relations
7778
* @return $this
7879
*/
79-
public function setRelations($relations)
80+
public function setRelations(RelationsDTO $relations)
8081
{
8182
$this->relations = $relations;
8283

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

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

src/Generators/MigrationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function generate(): void
2121
'class' => $this->getPluralName($this->model),
2222
'entity' => $this->model,
2323
'entities' => $entities,
24-
'relations' => $this->relations,
24+
'relations' => $this->relations->toArray(),
2525
'fields' => $this->fields,
2626
'table' => $this->generateTable($this->fields)
2727
]);

src/Generators/RequestsGenerator.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,18 @@ class RequestsGenerator extends EntityGenerator
1414
const DELETE_METHOD = 'Delete';
1515
const GET_METHOD = 'Get';
1616

17-
public function setRelations($relations)
18-
{
19-
parent::setRelations($relations);
20-
21-
$this->relations['belongsTo'] = array_map(function ($field) {
22-
return Str::snake($field) . '_id';
23-
}, $this->relations['belongsTo']);
24-
25-
return $this;
26-
}
17+
protected array $relationFields = [];
2718

2819
public function generate(): void
2920
{
3021
if (!$this->isStubExists('request')) {
3122
return;
3223
}
3324

25+
$this->relationFields = array_map(function ($field) {
26+
return Str::snake($field) . '_id';
27+
}, $this->relations->belongsTo);
28+
3429
if (in_array('R', $this->crudOptions)) {
3530
$this->createRequest(
3631
self::GET_METHOD,
@@ -177,7 +172,7 @@ protected function getRules($name, $type, $required, $nullable, $present): array
177172
Arr::get($replaces, $type, $type)
178173
];
179174

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

183178
$rules[] = "exists:{$this->getTableName($tableName)},id";

src/Generators/SeederGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function createEntitySeeder(): void
6060
{
6161
$content = "<?php \n\n" . $this->getStub('seeder', [
6262
'entity' => $this->model,
63-
'relations' => $this->relations,
63+
'relations' => $this->relations->toArray(),
6464
'namespace' => $this->getOrCreateNamespace('seeders'),
6565
'factoryNamespace' => $this->getOrCreateNamespace('factories'),
6666
]);

src/Generators/ServiceGenerator.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7+
use RonasIT\Support\DTO\RelationsDTO;
78
use RonasIT\Support\Exceptions\ClassNotExistsException;
89
use RonasIT\Support\Events\SuccessCreateMessage;
910

1011
class ServiceGenerator extends EntityGenerator
1112
{
12-
public function setRelations($relations)
13-
{
14-
foreach ($relations['belongsTo'] as $field) {
15-
$name = Str::snake($field) . '_id';
16-
17-
$this->fields['integer'][] = $name;
18-
}
19-
20-
return $this;
21-
}
22-
2313
public function generate(): void
2414
{
2515
if ($this->classExists('repositories', "{$this->model}Repository")) {

tests/FactoryGeneratorTest.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Config;
66
use Illuminate\Support\Facades\Event;
77
use Illuminate\View\ViewException;
8+
use RonasIT\Support\DTO\RelationsDTO;
89
use RonasIT\Support\Events\SuccessCreateMessage;
910
use RonasIT\Support\Events\WarningEvent;
1011
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
@@ -65,11 +66,7 @@ className: ViewException::class,
6566
->setFields([
6667
'another_type' => ['some_field'],
6768
])
68-
->setRelations([
69-
'hasOne' => [],
70-
'hasMany' => [],
71-
'belongsTo' => [],
72-
])
69+
->setRelations(new RelationsDTO())
7370
->setModel('Post')
7471
->generate();
7572
}
@@ -84,11 +81,10 @@ public function testCreateSuccess()
8481
'string' => ['title', 'iban', 'something'],
8582
'json' => ['json_text'],
8683
])
87-
->setRelations([
88-
'hasOne' => ['user'],
89-
'hasMany' => [],
90-
'belongsTo' => ['user'],
91-
])
84+
->setRelations(new RelationsDTO(
85+
hasOne: ['user'],
86+
belongsTo: ['user'],
87+
))
9288
->setModel('Post')
9389
->generate();
9490

@@ -112,11 +108,10 @@ public function testCreateFactoryWithoutFactoryStub(): void
112108
'string' => ['title', 'iban', 'something'],
113109
'json' => ['json_text'],
114110
])
115-
->setRelations([
116-
'hasOne' => ['user'],
117-
'hasMany' => [],
118-
'belongsTo' => ['user'],
119-
])
111+
->setRelations(new RelationsDTO(
112+
hasOne: ['user'],
113+
belongsTo: ['user'],
114+
))
120115
->setModel('Post')
121116
->generate();
122117

@@ -144,11 +139,10 @@ public function testConfigFolderWithIncorrectCase(): void
144139
'string' => ['title', 'iban', 'something'],
145140
'json' => ['json_text'],
146141
])
147-
->setRelations([
148-
'hasOne' => ['user'],
149-
'hasMany' => [],
150-
'belongsTo' => ['user'],
151-
])
142+
->setRelations(new RelationsDTO(
143+
hasOne: ['user'],
144+
belongsTo: ['user'],
145+
))
152146
->setModel('Post')
153147
->generate();
154148
}
@@ -165,11 +159,10 @@ public function testConfigFolderWithExtension(): void
165159
'string' => ['title', 'iban', 'something'],
166160
'json' => ['json_text'],
167161
])
168-
->setRelations([
169-
'hasOne' => ['user'],
170-
'hasMany' => [],
171-
'belongsTo' => ['user'],
172-
])
162+
->setRelations(new RelationsDTO(
163+
hasOne: ['user'],
164+
belongsTo: ['user'],
165+
))
173166
->setModel('Post')
174167
->generate();
175168

tests/MigrationGeneratorTest.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RonasIT\Support\Tests;
44

55
use Illuminate\Support\Carbon;
6+
use RonasIT\Support\DTO\RelationsDTO;
67
use RonasIT\Support\Events\WarningEvent;
78
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
89
use RonasIT\Support\Generators\MigrationGenerator;
@@ -18,12 +19,7 @@ className: UnknownFieldTypeException::class,
1819

1920
app(MigrationGenerator::class)
2021
->setModel('Post')
21-
->setRelations([
22-
'belongsTo' => [],
23-
'belongsToMany' => [],
24-
'hasOne' => [],
25-
'hasMany' => [],
26-
])
22+
->setRelations(new RelationsDTO())
2723
->setFields([
2824
'integer-required' => ['media_id', 'user_id'],
2925
'unknown-type' => ['title'],
@@ -37,12 +33,7 @@ public function testCreateMigration()
3733

3834
app(MigrationGenerator::class)
3935
->setModel('Post')
40-
->setRelations([
41-
'belongsTo' => [],
42-
'belongsToMany' => [],
43-
'hasOne' => [],
44-
'hasMany' => [],
45-
])
36+
->setRelations(new RelationsDTO())
4637
->setFields([
4738
'integer-required' => ['media_id', 'user_id'],
4839
'string' => ['title', 'body'],
@@ -62,12 +53,7 @@ public function testCreateMigrationMYSQL()
6253

6354
app(MigrationGenerator::class)
6455
->setModel('Post')
65-
->setRelations([
66-
'belongsTo' => [],
67-
'belongsToMany' => [],
68-
'hasOne' => [],
69-
'hasMany' => [],
70-
])
56+
->setRelations(new RelationsDTO())
7157
->setFields([
7258
'integer-required' => ['media_id', 'user_id'],
7359
'string' => ['title', 'body'],
@@ -88,12 +74,7 @@ public function testCreateMigrationWithoutMigrationStub(): void
8874

8975
app(MigrationGenerator::class)
9076
->setModel('Post')
91-
->setRelations([
92-
'belongsTo' => [],
93-
'belongsToMany' => [],
94-
'hasOne' => [],
95-
'hasMany' => [],
96-
])
77+
->setRelations(new RelationsDTO())
9778
->setFields([
9879
'integer-required' => ['media_id', 'user_id'],
9980
'string' => ['title', 'body'],

tests/ModelGeneratorTest.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RonasIT\Support\Tests;
44

5+
use RonasIT\Support\DTO\RelationsDTO;
56
use RonasIT\Support\Events\SuccessCreateMessage;
67
use RonasIT\Support\Events\WarningEvent;
78
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
@@ -39,12 +40,9 @@ className: ClassNotExistsException::class,
3940

4041
app(ModelGenerator::class)
4142
->setModel('Post')
42-
->setRelations([
43-
'hasOne' => ['Comment'],
44-
'hasMany' => [],
45-
'belongsTo' => [],
46-
'belongsToMany' => [],
47-
])
43+
->setRelations(new RelationsDTO(
44+
hasOne: ['Comment']
45+
))
4846
->generate();
4947
}
5048

@@ -58,12 +56,10 @@ public function testCreateModel()
5856
'integer-required' => ['media_id'],
5957
'boolean-required' => ['is_published'],
6058
])
61-
->setRelations([
62-
'hasOne' => ['Comment'],
63-
'hasMany' => ['User'],
64-
'belongsTo' => [],
65-
'belongsToMany' => [],
66-
])
59+
->setRelations(new RelationsDTO(
60+
hasOne: ['Comment'],
61+
hasMany: ['User'],
62+
))
6763
->generate();
6864

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

106102
app(ModelGenerator::class)
107103
->setModel('Post')
104+
->setRelations(new RelationsDTO())
108105
->setFields([])
109106
->generate();
110107

@@ -125,12 +122,10 @@ public function testCreateModelWithRelationsRelationStubNotExist()
125122
app(ModelGenerator::class)
126123
->setModel('Post')
127124
->setFields([])
128-
->setRelations([
129-
'hasOne' => ['Comment'],
130-
'hasMany' => ['User'],
131-
'belongsTo' => [],
132-
'belongsToMany' => [],
133-
])
125+
->setRelations(new RelationsDTO(
126+
hasOne: ['Comment'],
127+
hasMany: ['User'],
128+
))
134129
->generate();
135130

136131
$this->assertFileDoesNotExist('new_model.php', 'app/Models/Post.php');

0 commit comments

Comments
 (0)