@@ -43,7 +43,7 @@ public function variables(): array
43
43
'fillables ' => implode (', ' .infy_nl_tab (1 , 2 ), $ this ->generateFillables ()),
44
44
'casts ' => implode (', ' .infy_nl_tab (1 , 2 ), $ this ->generateCasts ()),
45
45
'rules ' => implode (', ' .infy_nl_tab (1 , 2 ), $ this ->generateRules ()),
46
- 'docs ' => $ this ->fillDocs (),
46
+ 'swaggerDocs ' => $ this ->fillDocs (),
47
47
'customPrimaryKey ' => $ this ->customPrimaryKey (),
48
48
'customCreatedAt ' => $ this ->customCreatedAt (),
49
49
'customUpdatedAt ' => $ this ->customUpdatedAt (),
@@ -121,94 +121,28 @@ private function fillDocs(): string
121
121
return '' ;
122
122
}
123
123
124
- $ templateData = $ this ->generateSwagger ('' );
125
-
126
- $ docsTemplate = get_template ('docs.model ' , 'laravel-generator ' );
127
- $ docsTemplate = fill_template ($ this ->config ->dynamicVars , $ docsTemplate );
128
-
129
- $ fillables = '' ;
130
- $ fieldsArr = [];
131
- $ count = 1 ;
132
- if (isset ($ this ->config ->relations ) && !empty ($ this ->config ->relations )) {
133
- foreach ($ this ->config ->relations as $ relation ) {
134
- $ field = $ relationText = (isset ($ relation ->inputs [0 ])) ? $ relation ->inputs [0 ] : null ;
135
- if (in_array ($ field , $ fieldsArr )) {
136
- $ relationText = $ relationText .'_ ' .$ count ;
137
- $ count ++;
138
- }
139
-
140
- $ fillables .= ' * @property ' .$ this ->getPHPDocType ($ relation ->type , $ relation , $ relationText ).infy_nl ();
141
- $ fieldsArr [] = $ field ;
142
- }
143
- }
144
-
145
- if (isset ($ this ->config ->fields ) && !empty ($ this ->config ->fields )) {
146
- foreach ($ this ->config ->fields as $ field ) {
147
- if ($ field ->isFillable ) {
148
- $ fillables .= ' * @property ' .$ this ->getPHPDocType ($ field ->fieldType ).' $ ' .$ field ->name .infy_nl ();
149
- }
150
- }
151
- }
152
-
153
- $ docsTemplate = str_replace ('$GENERATE_DATE$ ' , date ('F j, Y, g:i a T ' ), $ docsTemplate );
154
- $ docsTemplate = str_replace ('$PHPDOC$ ' , $ fillables , $ docsTemplate );
155
-
156
- return str_replace ('$DOCS$ ' , $ docsTemplate , $ templateData );
124
+ return $ this ->generateSwagger ();
157
125
}
158
126
159
- private function getPHPDocType ( string $ dbType , GeneratorFieldRelation $ relation = null , string $ relationText = null ): string
127
+ public function generateSwagger ( ): string
160
128
{
161
- $ relationText = (!empty ($ relationText )) ? $ relationText : null ;
162
- $ modelNamespace = $ this ->config ->namespaces ->model ;
163
-
164
- switch ($ dbType ) {
165
- case 'datetime ' :
166
- return 'string|\Carbon\Carbon ' ;
167
- case '1t1 ' :
168
- return '\\' .$ modelNamespace .'\\' .$ relation ->inputs [0 ].' $ ' .Str::camel ($ relationText );
169
- case 'mt1 ' :
170
- if (isset ($ relation ->inputs [1 ])) {
171
- $ relationName = str_replace ('_id ' , '' , strtolower ($ relation ->inputs [1 ]));
172
- } else {
173
- $ relationName = $ relationText ;
174
- }
175
-
176
- return '\\' .$ modelNamespace .'\\' .$ relation ->inputs [0 ].' $ ' .Str::camel ($ relationName );
177
- case '1tm ' :
178
- case 'mtm ' :
179
- case 'hmt ' :
180
- return '\Illuminate\Database\Eloquent\Collection $ ' .Str::camel (Str::plural ($ relationText ));
181
- default :
182
- $ fieldData = SwaggerGenerator::getFieldType ($ dbType );
183
- if (!empty ($ fieldData ['fieldType ' ])) {
184
- return $ fieldData ['fieldType ' ];
185
- }
186
-
187
- return $ dbType ;
188
- }
189
- }
129
+ $ requiredFields = $ this ->generateRequiredFields ();
190
130
191
- public function generateSwagger ($ templateData ): string
192
- {
193
131
$ fieldTypes = SwaggerGenerator::generateTypes ($ this ->config ->fields );
194
132
195
- $ template = get_template ('model_docs.model ' , 'swagger-generator ' );
196
-
197
- $ template = fill_template ($ this ->config ->dynamicVars , $ template );
198
-
199
- $ template = str_replace (
200
- '$REQUIRED_FIELDS$ ' ,
201
- '" ' .implode ('" ' .', ' .'" ' , $ this ->generateRequiredFields ()).'" ' ,
202
- $ template
203
- );
204
-
205
- $ propertyTemplate = get_template ('model_docs.property ' , 'swagger-generator ' );
206
-
207
- $ properties = SwaggerGenerator::preparePropertyFields ($ propertyTemplate , $ fieldTypes );
133
+ $ properties = [];
134
+ foreach ($ fieldTypes as $ fieldType ) {
135
+ $ properties [] = view (
136
+ 'swagger-generator::model.property ' , $ fieldType
137
+ )->render ();
138
+ }
208
139
209
- $ template = str_replace ( ' $PROPERTIES$ ' , implode (" , \n" , $ properties ), $ template ) ;
140
+ $ requiredFields = ' { ' . implode (' , ' , $ requiredFields ). ' } ' ;
210
141
211
- return str_replace ('$DOCS$ ' , $ template , $ templateData );
142
+ return view ('swagger-generator::model.model ' , [
143
+ 'requiredFields ' => $ requiredFields ,
144
+ 'properties ' => implode (", " .infy_nl ().' ' , $ properties ),
145
+ ]);
212
146
}
213
147
214
148
private function generateRequiredFields (): array
@@ -219,7 +153,7 @@ private function generateRequiredFields(): array
219
153
foreach ($ this ->config ->fields as $ field ) {
220
154
if (!empty ($ field ->validations )) {
221
155
if (Str::contains ($ field ->validations , 'required ' )) {
222
- $ requiredFields [] = $ field ->name ;
156
+ $ requiredFields [] = ' " ' . $ field ->name . ' " ' ;
223
157
}
224
158
}
225
159
}
0 commit comments