1010
1111class ModelGenerator extends EntityGenerator
1212{
13- CONST PLURAL_NUMBER_REQUIRED = [
13+ protected const array PLURAL_NUMBER_REQUIRED = [
1414 'belongsToMany ' ,
15- 'hasMany '
15+ 'hasMany ' ,
1616 ];
1717
1818 public function generate (): void
1919 {
2020 if ($ this ->classExists ('models ' , $ this ->model )) {
2121 $ this ->throwFailureException (
22- ClassAlreadyExistsException::class,
23- "Cannot create {$ this ->model } Model cause {$ this ->model } Model already exists. " ,
24- "Remove {$ this ->model } Model. "
22+ exceptionClass: ClassAlreadyExistsException::class,
23+ failureMessage: "Cannot create {$ this ->model } Model cause {$ this ->model } Model already exists. " ,
24+ recommendedMessage: "Remove {$ this ->model } Model. " ,
2525 );
2626 }
2727
28- if ($ this ->isStubExists ('model ' ) && ($ this ->isStubExists ( ' relation ' ) || empty ( $ this ->relations ))) {
28+ if ($ this ->isStubExists ('model ' ) && (! $ this ->hasRelations ( ) || $ this ->isStubExists ( ' relation ' , ' model ' ))) {
2929 $ this ->prepareRelatedModels ();
3030 $ modelContent = $ this ->getNewModelContent ();
3131
@@ -35,14 +35,19 @@ public function generate(): void
3535 }
3636 }
3737
38+ protected function hasRelations (): bool
39+ {
40+ return !collect ($ this ->relations )->every (fn ($ relation ) => empty ($ relation ));
41+ }
42+
3843 protected function getNewModelContent (): string
3944 {
4045 return $ this ->getStub ('model ' , [
4146 'entity ' => $ this ->model ,
4247 'fields ' => Arr::collapse ($ this ->fields ),
4348 'relations ' => $ this ->prepareRelations (),
4449 'casts ' => $ this ->getCasts ($ this ->fields ),
45- 'namespace ' => $ this ->getOrCreateNamespace ('models ' )
50+ 'namespace ' => $ this ->getOrCreateNamespace ('models ' ),
4651 ]);
4752 }
4853
@@ -59,9 +64,9 @@ public function prepareRelatedModels(): void
5964 foreach ($ relationsByType as $ relation ) {
6065 if (!$ this ->classExists ('models ' , $ relation )) {
6166 $ this ->throwFailureException (
62- ClassNotExistsException::class,
63- "Cannot create {$ relation } Model cause {$ relation } Model does not exists . " ,
64- "Create a {$ relation } Model by himself or run command 'php artisan make:entity {$ relation } --only-model'. "
67+ exceptionClass: ClassNotExistsException::class,
68+ failureMessage: "Cannot create {$ this -> model } Model cause relation model {$ relation } does not exist . " ,
69+ recommendedMessage: "Create the {$ relation } Model by himself or run command 'php artisan make:entity {$ relation } --only-model'. " ,
6570 );
6671 }
6772
@@ -70,7 +75,7 @@ public function prepareRelatedModels(): void
7075 $ newRelation = $ this ->getStub ('relation ' , [
7176 'name ' => $ this ->getRelationName ($ this ->model , $ types [$ type ]),
7277 'type ' => $ types [$ type ],
73- 'entity ' => $ this ->model
78+ 'entity ' => $ this ->model ,
7479 ]);
7580
7681 $ fixedContent = preg_replace ('/\}$/ ' , "\n {$ newRelation }\n} " , $ content );
@@ -80,9 +85,9 @@ public function prepareRelatedModels(): void
8085 }
8186 }
8287
83- public function getModelContent ($ model ): string
88+ public function getModelContent (string $ model ): string
8489 {
85- $ modelPath = base_path ($ this ->paths ['models ' ] . " / {$ model }.php " );
90+ $ modelPath = base_path ("{ $ this ->paths ['models ' ]} / {$ model }.php " );
8691
8792 return file_get_contents ($ modelPath );
8893 }
@@ -97,7 +102,7 @@ public function prepareRelations(): array
97102 $ result [] = [
98103 'name ' => $ this ->getRelationName ($ relation , $ type ),
99104 'type ' => $ type ,
100- 'entity ' => $ relation
105+ 'entity ' => $ relation,
101106 ];
102107 }
103108 }
@@ -106,7 +111,7 @@ public function prepareRelations(): array
106111 return $ result ;
107112 }
108113
109- protected function getCasts ($ fields ): array
114+ protected function getCasts (array $ fields ): array
110115 {
111116 $ casts = [
112117 'boolean-required ' => 'boolean ' ,
@@ -117,7 +122,7 @@ protected function getCasts($fields): array
117122 $ result = [];
118123
119124 foreach ($ fields as $ fieldType => $ names ) {
120- if (empty ( $ casts [ $ fieldType] )) {
125+ if (! array_key_exists ( $ fieldType, $ casts )) {
121126 continue ;
122127 }
123128
@@ -129,7 +134,7 @@ protected function getCasts($fields): array
129134 return $ result ;
130135 }
131136
132- private function getRelationName ($ relation , $ type ): string
137+ private function getRelationName (string $ relation , string $ type ): string
133138 {
134139 $ relationName = Str::snake ($ relation );
135140
0 commit comments