20
20
use function output ;
21
21
use function rtrim ;
22
22
use function sprintf ;
23
+ use function str_replace ;
23
24
use function strpos ;
24
25
use function ucfirst ;
25
- use function str_replace ;
26
26
27
27
/**
28
28
* EntityLogic
@@ -84,14 +84,16 @@ private function generateEntity(
84
84
string $ fieldPrefix ,
85
85
string $ tplDir
86
86
): void {
87
+ $ file = alias ($ path );
88
+ $ tplDir = alias ($ tplDir );
89
+
87
90
$ mappingClass = $ tableSchema ['mapping ' ];
88
91
$ config = [
89
92
'tplFilename ' => 'entity ' ,
90
93
'tplDir ' => $ tplDir ,
91
94
'className ' => $ mappingClass ,
92
95
];
93
96
94
- $ file = alias ($ path );
95
97
if (!is_dir ($ file )) {
96
98
if (!$ isConfirm && !ConsoleHelper::confirm ("mkdir path $ file, Ensure continue? " , true )) {
97
99
output ()->writeln (' Quit, Bye! ' );
@@ -107,8 +109,7 @@ private function generateEntity(
107
109
$ genGetters = [];
108
110
$ genProperties = [];
109
111
foreach ($ columnSchemas as $ columnSchema ) {
110
- $ propertyCode = $ this ->generateProperties ($ columnSchema , $ tplDir );
111
- $ genProperties [] = $ propertyCode ;
112
+ $ genProperties [] = $ this ->generateProperties ($ columnSchema , $ tplDir );
112
113
113
114
$ genSetters [] = $ this ->generateSetters ($ columnSchema , $ tplDir );
114
115
$ genGetters [] = $ this ->generateGetters ($ columnSchema , $ tplDir );
@@ -126,14 +127,15 @@ private function generateEntity(
126
127
'entityName ' => $ mappingClass ,
127
128
'namespace ' => $ this ->getNameSpace ($ path ),
128
129
'tableComment ' => $ tableSchema ['comment ' ],
129
- 'dbPool ' => $ pool == Pool::DEFAULT_POOL ? '' : ',pool=" ' . $ pool . '" ' ,
130
+ 'dbPool ' => $ pool == Pool::DEFAULT_POOL ? '' : ', pool=" ' . $ pool . '" ' ,
130
131
];
131
132
$ gen = new FileGenerator ($ config );
132
133
133
134
if (!$ isConfirm && !ConsoleHelper::confirm ("generate entity $ file, Ensure continue? " , true )) {
134
135
output ()->writeln (' Quit, Bye! ' );
135
136
return ;
136
137
}
138
+
137
139
if ($ gen ->renderas ($ file , $ data )) {
138
140
output ()->colored (" Generate entity $ file OK! " , 'success ' );
139
141
return ;
@@ -169,33 +171,43 @@ private function generateProperties(array $colSchema, string $tplDir): string
169
171
'tplFilename ' => 'property ' ,
170
172
'tplDir ' => $ tplDir ,
171
173
];
174
+
172
175
// id
173
176
$ id = '* ' ;
174
177
if (!empty ($ colSchema ['key ' ]) && !$ this ->readyGenerateId ) {
175
178
// Is auto increment
176
179
$ auto = $ colSchema ['extra ' ] && strpos ($ colSchema ['extra ' ], 'auto_increment ' ) !== false ?
177
180
'' :
178
181
'incrementing=false ' ;
182
+
179
183
// builder @id
180
184
$ id = "* @Id( $ auto) " ;
181
185
$ this ->readyGenerateId = true ;
182
186
}
183
- // Is need map
184
- $ prop = $ colSchema ['mappingName ' ] == $ colSchema ['name ' ] ? '' :
185
- sprintf ('prop="%s" ' , $ colSchema ['mappingName ' ]);
187
+
188
+ $ mappingName = $ colSchema ['mappingName ' ];
189
+ $ fieldName = $ colSchema ['name ' ];
190
+
191
+ // is need map
192
+ $ prop = $ mappingName == $ fieldName ? '' :
193
+ sprintf ('prop="%s" ' , $ mappingName );
194
+
186
195
// column name
187
- $ columnName = $ colSchema ['name ' ] == $ colSchema ['mappingName ' ] ? '' :
188
- sprintf ('name="%s" ' , $ colSchema ['name ' ]);
196
+ $ columnName = $ mappingName == $ fieldName ? '' :
197
+ sprintf ('name="%s" ' , $ fieldName );
198
+
189
199
// is need hidden
190
- $ hidden = in_array ($ colSchema ['mappingName ' ], ['password ' , 'pwd ' ]) ? "hidden=true " : '' ;
200
+ $ hidden = in_array ($ mappingName , ['password ' , 'pwd ' ]) ? "hidden=true " : '' ;
201
+
191
202
$ columnDetail = array_filter ([$ columnName , $ prop , $ hidden ]);
192
203
$ data = [
193
204
'type ' => $ colSchema ['phpType ' ],
194
- 'propertyName ' => sprintf ('$%s ' , $ colSchema [ ' mappingName ' ] ),
195
- 'columnDetail ' => $ columnDetail ? implode (', ' , $ columnDetail ) : '' ,
205
+ 'propertyName ' => sprintf ('$%s ' , $ mappingName ),
206
+ 'columnDetail ' => $ columnDetail ? implode (', ' , $ columnDetail ) : '' ,
196
207
'id ' => $ id ,
197
208
'comment ' => $ colSchema ['columnComment ' ],
198
209
];
210
+
199
211
$ gen = new FileGenerator ($ entityConfig );
200
212
$ propertyCode = $ gen ->render ($ data );
201
213
@@ -212,17 +224,18 @@ private function generateProperties(array $colSchema, string $tplDir): string
212
224
private function generateGetters (array $ colSchema , string $ tplDir ): string
213
225
{
214
226
$ getterName = sprintf ('get%s ' , ucfirst ($ colSchema ['mappingName ' ]));
215
-
216
- $ config = [
227
+ $ type = $ colSchema [ ' is_nullable ' ] ? ' ? ' . $ colSchema [ ' originPHPType ' ] : $ colSchema [ ' originPHPType ' ];
228
+ $ config = [
217
229
'tplFilename ' => 'getter ' ,
218
230
'tplDir ' => $ tplDir ,
219
231
];
220
- $ data = [
232
+ $ data = [
233
+ 'type ' => $ type ,
221
234
'returnType ' => $ colSchema ['phpType ' ],
222
235
'methodName ' => $ getterName ,
223
236
'property ' => $ colSchema ['mappingName ' ],
224
237
];
225
- $ gen = new FileGenerator ($ config );
238
+ $ gen = new FileGenerator ($ config );
226
239
227
240
return (string )$ gen ->render ($ data );
228
241
}
@@ -248,7 +261,7 @@ private function generateSetters(array $colSchema, string $tplDir): string
248
261
'type ' => $ type ,
249
262
'paramType ' => $ colSchema ['phpType ' ],
250
263
'methodName ' => $ setterName ,
251
- 'paramName ' => ' $ ' . $ colSchema ['mappingName ' ],
264
+ 'paramName ' => sprintf ( ' $%s ' , $ colSchema ['mappingName ' ]) ,
252
265
'property ' => $ colSchema ['mappingName ' ],
253
266
];
254
267
$ gen = new FileGenerator ($ config );
0 commit comments