@@ -36,15 +36,18 @@ protected function hasRelations(): bool
3636
3737 protected function getNewModelContent (): string
3838 {
39+ $ relations = $ this ->prepareRelations ();
40+
3941 return $ this ->getStub ('model ' , [
4042 'entity ' => $ this ->model ,
4143 'fields ' => Arr::collapse ($ this ->fields ),
42- 'relations ' => $ this -> prepareRelations () ,
44+ 'relations ' => $ relations ,
4345 'casts ' => $ this ->getCasts ($ this ->fields ),
4446 'namespace ' => $ this ->generateNamespace ($ this ->paths ['models ' ], $ this ->modelSubFolder ),
4547 'importRelations ' => $ this ->getImportedRelations (),
46- 'anotationProperties ' => $ this ->generateAnnotationProperties ($ this ->fields ),
48+ 'annotationProperties ' => $ this ->generateAnnotationProperties ($ this ->fields , $ relations ),
4749 'hasCarbonField ' => !empty ($ this ->fields ['timestamp ' ]) || !empty ($ this ->fields ['timestamp-required ' ]),
50+ 'hasCollectionType ' => !empty ($ this ->relations ->hasMany ) || !empty ($ this ->relations ->belongsToMany ),
4851 ]);
4952 }
5053
@@ -68,14 +71,19 @@ public function prepareRelatedModels(): void
6871 $ this ->insertImport ($ content , $ namespace );
6972 }
7073
74+ $ relationName = $ this ->getRelationName ($ this ->model , $ types [$ type ]);
75+
7176 $ newRelation = $ this ->getStub ('relation ' , [
72- 'name ' => $ this -> getRelationName ( $ this -> model , $ types [ $ type ]) ,
77+ 'name ' => $ relationName ,
7378 'type ' => $ types [$ type ],
7479 'entity ' => $ this ->model ,
7580 ]);
7681
82+ // TODO: use ronasit/larabuilder instead regexp
7783 $ fixedContent = preg_replace ('/\}$/ ' , "\n {$ newRelation }\n} " , $ content );
7884
85+ $ this ->insertPropertyAnnotation ($ fixedContent , $ this ->getRelationType ($ this ->model , $ types [$ type ]), $ relationName );
86+
7987 $ this ->saveClass ('models ' , $ relation , $ fixedContent );
8088 }
8189 }
@@ -93,6 +101,7 @@ protected function insertImport(string &$classContent, string $import): void
93101 $ import = "use {$ import }; " ;
94102
95103 if (!Str::contains ($ classContent , $ import )) {
104+ // TODO: use ronasit/larabuilder instead regexp
96105 $ classContent = preg_replace ('/(namespace\s+[^;]+;\s*)/ ' , "$1 {$ import }\n" , $ classContent , 1 );
97106 }
98107 }
@@ -182,7 +191,7 @@ protected function generateClassNamespace(string $className, ?string $folder = n
182191 return "{$ path }\\{$ psrPath }" ;
183192 }
184193
185- protected function generateAnnotationProperties (array $ fields ): array
194+ protected function generateAnnotationProperties (array $ fields, array $ relations ): array
186195 {
187196 $ result = [];
188197
@@ -192,6 +201,10 @@ protected function generateAnnotationProperties(array $fields): array
192201 }
193202 }
194203
204+ foreach ($ relations as $ relation ) {
205+ $ result [$ relation ['name ' ]] = $ this ->getRelationType ($ relation ['entity ' ], $ relation ['type ' ]);
206+ }
207+
195208 return $ result ;
196209 }
197210
@@ -231,4 +244,29 @@ protected function isRequired(string $typeName): bool
231244 {
232245 return Str::endsWith ($ typeName , 'required ' );
233246 }
247+
248+ protected function getRelationType (string $ model , string $ relation ): string
249+ {
250+ if (in_array ($ relation , self ::PLURAL_NUMBER_REQUIRED )) {
251+ return "Collection< {$ model }> " ;
252+ }
253+
254+ return "{$ model }|null " ;
255+ }
256+
257+ protected function insertPropertyAnnotation (string &$ content , string $ propertyDataType , string $ propertyName ): void
258+ {
259+ $ annotation = "* @property {$ propertyDataType } \${$ propertyName }" ;
260+
261+ // TODO: use ronasit/larabuilder instead regexp
262+ if (!Str::contains ($ content , '/** ' )) {
263+ $ content = preg_replace ('/^\s*class[\s\S]+?\{/m ' , "\n/** \n {$ annotation }\n */$0 " , $ content );
264+ } else {
265+ $ content = preg_replace ('/\*\//m ' , "{$ annotation }\n $0 " , $ content );
266+ }
267+
268+ if (Str::contains ($ propertyDataType , 'Collection ' )) {
269+ $ this ->insertImport ($ content , 'Illuminate\Database\Eloquent\Collection ' );
270+ }
271+ }
234272}
0 commit comments