2
2
3
3
namespace Eghamat24 \DatabaseRepository \Creators ;
4
4
5
+ use Eghamat24 \DatabaseRepository \Models \Enums \DataTypeEnum ;
5
6
use Illuminate \Support \Collection ;
6
7
use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
7
8
@@ -31,25 +32,21 @@ public function getExtendSection(): string
31
32
32
33
public function createAttributes (): array
33
34
{
34
- $ columns = $ this ->columns ;
35
- $ entityStubsPath = $ this ->entityStubsPath ;
36
- $ detectForeignKeys = $ this ->detectForeignKeys ;
37
- $ tableName = $ this ->tableName ;
38
35
$ attributes = [];
39
36
40
- foreach ($ columns as $ _column ) {
37
+ foreach ($ this -> columns as $ _column ) {
41
38
42
39
$ dataType = $ this ->getDataType ($ _column ->COLUMN_TYPE , $ _column ->DATA_TYPE );
43
40
44
41
$ defaultValue = null ;
45
42
if ($ _column ->COLUMN_DEFAULT !== null ) {
46
43
$ defaultValue = $ _column ->COLUMN_DEFAULT ;
47
44
48
- if ($ dataType == ' int ' ) {
45
+ if ($ dataType === DataTypeEnum:: INTEGER_TYPE ) {
49
46
$ defaultValue = intval ($ defaultValue );
50
47
}
51
48
52
- if ($ dataType == self ::BOOL_TYPE ) {
49
+ if ($ dataType === self ::BOOL_TYPE ) {
53
50
if (in_array ($ defaultValue , [0 , '' , "'' " ])) {
54
51
$ defaultValue = 'false ' ;
55
52
} elseif (in_array ($ defaultValue , [1 , '1 ' ])) {
@@ -59,29 +56,31 @@ public function createAttributes(): array
59
56
}
60
57
61
58
$ columnString = $ _column ->COLUMN_NAME ;
59
+
62
60
if (!in_array ($ _column ->COLUMN_DEFAULT , [null , 'NULL ' ])) {
63
61
$ columnString .= ' = ' . $ defaultValue ;
64
62
}
63
+
65
64
if ($ _column ->IS_NULLABLE === 'YES ' ) {
66
65
$ columnString .= ' = null ' ;
67
66
}
68
67
69
68
$ attributes [$ _column ->COLUMN_NAME ] =
70
69
$ this ->writeAttribute (
71
- $ entityStubsPath ,
70
+ $ this -> entityStubsPath ,
72
71
$ columnString ,
73
72
($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ dataType
74
73
);
75
74
}
76
75
77
- if ($ detectForeignKeys ) {
78
- $ foreignKeys = $ this ->extractForeignKeys ($ tableName );
76
+ if ($ this -> detectForeignKeys ) {
77
+ $ foreignKeys = $ this ->extractForeignKeys ($ this -> tableName );
79
78
80
79
// Create Additional Attributes from Foreign Keys
81
80
foreach ($ foreignKeys as $ _foreignKey ) {
82
81
$ attributes [$ _column ->COLUMN_NAME ] =
83
82
$ this ->writeAttribute (
84
- $ entityStubsPath ,
83
+ $ this -> entityStubsPath ,
85
84
$ _foreignKey ->VARIABLE_NAME ,
86
85
$ _foreignKey ->ENTITY_DATA_TYPE
87
86
);
@@ -93,73 +92,85 @@ public function createAttributes(): array
93
92
94
93
public function createUses (): array
95
94
{
96
- return [" use Eghamat24\DatabaseRepository\Models\Entity\Entity; " ];
95
+ return [' use Eghamat24\DatabaseRepository\Models\Entity\Entity; ' ];
97
96
}
98
97
99
98
public function createFunctions (): array
100
99
{
101
- $ columns = $ this ->columns ;
102
- $ entityStubsPath = $ this ->entityStubsPath ;
103
- $ detectForeignKeys = $ this ->detectForeignKeys ;
104
- $ tableName = $ this ->tableName ;
105
100
$ settersAndGetters = [];
106
- foreach ($ columns as $ _column ) {
101
+
102
+ foreach ($ this ->columns as $ _column ) {
107
103
$ dataType = $ this ->getDataType ($ _column ->COLUMN_TYPE , $ _column ->DATA_TYPE );
108
104
109
105
$ settersAndGetters ['get ' . ucwords ($ _column ->COLUMN_NAME )] =
110
106
$ this ->writeAccessors (
111
- $ entityStubsPath ,
107
+ $ this -> entityStubsPath ,
112
108
$ _column ->COLUMN_NAME ,
113
109
($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ dataType ,
114
110
'getter '
115
111
);
112
+
116
113
$ settersAndGetters ['set ' . ucwords ($ _column ->COLUMN_NAME )] =
117
114
$ this ->writeAccessors (
118
- $ entityStubsPath ,
115
+ $ this -> entityStubsPath ,
119
116
$ _column ->COLUMN_NAME ,
120
117
($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ dataType ,
121
118
'setter '
122
119
);
123
120
124
121
}
125
- if ($ detectForeignKeys ) {
126
- $ foreignKeys = $ this ->extractForeignKeys ($ tableName );
122
+
123
+ if ($ this ->detectForeignKeys ) {
124
+ $ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
127
125
128
126
// Create Additional Setters and Getters from Foreign keys
129
127
foreach ($ foreignKeys as $ _foreignKey ) {
128
+
130
129
$ settersAndGetters ['get ' . ucwords ($ _foreignKey ->COLUMN_NAME )] =
131
130
$ this ->writeAccessors (
132
- $ entityStubsPath ,
131
+ $ this -> entityStubsPath ,
133
132
$ _foreignKey ->VARIABLE_NAME ,
134
133
$ _foreignKey ->ENTITY_DATA_TYPE ,
135
134
'getter '
136
135
);
136
+
137
137
$ settersAndGetters ['set ' . ucwords ($ _foreignKey ->COLUMN_NAME )] =
138
138
$ this ->writeAccessors (
139
- $ entityStubsPath ,
139
+ $ this -> entityStubsPath ,
140
140
$ _foreignKey ->VARIABLE_NAME ,
141
141
$ _foreignKey ->ENTITY_DATA_TYPE ,
142
142
'setter '
143
143
);
144
144
}
145
145
}
146
+
146
147
return $ settersAndGetters ;
147
148
}
148
149
149
150
private function writeAttribute (string $ entityStubsPath , string $ attributeName , string $ attributeType ): string
150
151
{
151
152
$ attributeStub = file_get_contents ($ entityStubsPath . 'attribute.stub ' );
152
- return str_replace (['{{ AttributeType }} ' , '{{ AttributeName }} ' ],
153
- [$ attributeType , $ attributeName ],
154
- $ attributeStub );
153
+
154
+ $ replaceMapping = [
155
+ '{{ AttributeType }} ' => $ attributeType ,
156
+ '{{ AttributeName }} ' => $ attributeName ,
157
+ ];
158
+
159
+ return str_replace (array_keys ($ replaceMapping ), array_values ($ replaceMapping ), $ attributeStub );
155
160
}
156
161
157
162
private function writeAccessors (string $ entityStubsPath , string $ attributeName , string $ attributeType , string $ type ): string
158
163
{
159
164
$ accessorStub = file_get_contents ($ entityStubsPath . $ type . '.stub ' );
160
- return str_replace (['{{ AttributeType }} ' , '{{ AttributeName }} ' , '{{ GetterName }} ' , '{{ SetterName }} ' ],
161
- [$ attributeType , $ attributeName , ucfirst ($ attributeName ), ucfirst ($ attributeName )],
162
- $ accessorStub );
165
+
166
+ $ replaceMapping = [
167
+ '{{ AttributeType }} ' => $ attributeType ,
168
+ '{{ AttributeName }} ' => $ attributeName ,
169
+ '{{ GetterName }} ' => ucfirst ($ attributeName ),
170
+ '{{ SetterName }} ' => ucfirst ($ attributeName )
171
+ ];
172
+
173
+ return str_replace (array_keys ($ replaceMapping ), array_values ($ replaceMapping ), $ accessorStub );
163
174
}
164
175
165
176
public function getNameSpace (): string
0 commit comments