1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hechoenlaravel\JarvisFoundation\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait EntityManager |
7
|
|
|
* Use it to Manage Entities and fields in your classes |
8
|
|
|
* @author Jose Luis Fonseca <[email protected]> |
9
|
|
|
* @package Hechoenlaravel\JarvisFoundation\Traits |
10
|
|
|
*/ |
11
|
|
|
trait EntityManager |
12
|
|
|
{ |
13
|
|
|
use DispatchesCommands; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Generate an entity |
17
|
|
|
* @param array $data |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function generateEntity(array $data = []) |
21
|
|
|
{ |
22
|
|
|
return $this->execute('Hechoenlaravel\JarvisFoundation\EntityGenerator\EntityGeneratorCommand', |
23
|
|
|
'Hechoenlaravel\JarvisFoundation\EntityGenerator\Handler\EntityGeneratorHandler', $data, [ |
24
|
|
|
'Hechoenlaravel\JarvisFoundation\EntityGenerator\Middleware\EntityGeneratorValidator', |
25
|
|
|
'Hechoenlaravel\JarvisFoundation\EntityGenerator\Middleware\SetPrefixAndTableName' |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Generate a field for an entity |
31
|
|
|
* @param array $data |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function generateField(array $data = []) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
return $this->execute('Hechoenlaravel\JarvisFoundation\FieldGenerator\CreateFieldCommand', |
37
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Handler\CreateFieldCommandHandler', $data, [ |
38
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\FieldGeneratorValidator', |
39
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\FieldTypeValidator', |
40
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\FieldOrderSetter', |
41
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\CreateTheFieldSlug', |
42
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\CallPreAssignEventOnField', |
43
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\FieldOptionsSerializer', |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Edit a field |
49
|
|
|
* @param array $data |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function editField(array $data = []) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return $this->execute('Hechoenlaravel\JarvisFoundation\FieldGenerator\EditFieldCommand', |
55
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Handler\EditFieldCommandHandler', $data, [ |
56
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\EditFieldTypeValidator', |
57
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\CreateTheFieldSlug', |
58
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\SetFieldTypeOnEdit', |
59
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\SetCommandDataFromEditFieldModel', |
60
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\CallPreAssignEventOnField', |
61
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Middleware\FieldOptionsSerializer', |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Delete a Field |
67
|
|
|
* @param $id |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function deleteField($id) |
71
|
|
|
{ |
72
|
|
|
return $this->execute('Hechoenlaravel\JarvisFoundation\FieldGenerator\DeleteFieldCommand', |
73
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Handler\DeleteFieldCommandHandler', [ |
74
|
|
|
'id' => $id |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Re order from a field |
80
|
|
|
* @param $id |
81
|
|
|
* @param $order |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function reOrderField($items) |
85
|
|
|
{ |
86
|
|
|
return $this->execute('Hechoenlaravel\JarvisFoundation\FieldGenerator\ReOrderFieldCommand', |
87
|
|
|
'Hechoenlaravel\JarvisFoundation\FieldGenerator\Handler\ReOrderFieldCommandHandler', [ |
88
|
|
|
'fields' => $items |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.