33namespace RonasIT \Support \Generators ;
44
55use Carbon \Carbon ;
6- use Exception ;
6+ use Illuminate \ Support \ Str ;
77use RonasIT \Support \Events \SuccessCreateMessage ;
8+ use RonasIT \Support \Exceptions \UnknownFieldTypeException ;
89
910class MigrationGenerator extends EntityGenerator
1011{
@@ -24,29 +25,30 @@ public function generate(): void
2425 'fields ' => $ this ->fields ,
2526 'table ' => $ this ->generateTable ($ this ->fields )
2627 ]);
28+
2729 $ now = Carbon::now ()->format ('Y_m_d_His ' );
2830
2931 $ this ->saveClass ('migrations ' , "{$ now }_ {$ entities }_create_table " , $ content );
3032
3133 event (new SuccessCreateMessage ("Created a new Migration: {$ entities }_create_table " ));
3234 }
3335
34- protected function isJson ($ typeName ): bool
36+ protected function isJson (string $ typeName ): bool
3537 {
36- return $ typeName == 'json ' ;
38+ return $ typeName === 'json ' ;
3739 }
3840
39- protected function isRequired ($ typeName ): bool
41+ protected function isRequired (string $ typeName ): bool
4042 {
41- return ! empty ( explode ( '- ' , $ typeName )[ 1 ]) ;
43+ return Str:: afterLast ( $ typeName , '- ' ) === ' required ' ;
4244 }
4345
44- protected function isNullable ($ typeName ): bool
46+ protected function isNullable (string $ typeName ): bool
4547 {
4648 return empty (explode ('- ' , $ typeName )[1 ]);
4749 }
4850
49- protected function getJsonLine ($ fieldName ): string
51+ protected function getJsonLine (string $ fieldName ): string
5052 {
5153 if (env ("DB_CONNECTION " ) == "mysql " ) {
5254 return "\$table->json(' {$ fieldName }')->nullable(); " ;
@@ -55,7 +57,7 @@ protected function getJsonLine($fieldName): string
5557 return "\$table->jsonb(' {$ fieldName }')->default( \"{} \"); " ;
5658 }
5759
58- protected function getRequiredLine ($ fieldName , $ typeName ): string
60+ protected function getRequiredLine (string $ fieldName , string $ typeName ): string
5961 {
6062 $ type = explode ('- ' , $ typeName )[0 ];
6163
@@ -66,27 +68,27 @@ protected function getRequiredLine($fieldName, $typeName): string
6668 return "\$table-> {$ type }(' {$ fieldName }'); " ;
6769 }
6870
69- protected function getNonRequiredLine ($ fieldName , $ typeName ): string
71+ protected function getNonRequiredLine (string $ fieldName , string $ typeName ): string
7072 {
7173 $ type = explode ('- ' , $ typeName )[0 ];
7274
7375 return "\$table-> {$ type }(' {$ fieldName }')->nullable(); " ;
7476 }
7577
76- protected function generateTable ($ fields ): array
78+ protected function generateTable (array $ fields ): array
7779 {
7880 $ resultTable = [];
7981
8082 foreach ($ fields as $ typeName => $ fieldNames ) {
8183 foreach ($ fieldNames as $ fieldName ) {
82- array_push ( $ resultTable, $ this ->getTableRow ($ fieldName , $ typeName) );
84+ $ resultTable[] = $ this ->getTableRow ($ fieldName , $ typeName );
8385 }
8486 }
8587
8688 return $ resultTable ;
8789 }
8890
89- protected function getTableRow ($ fieldName , $ typeName ): string
91+ protected function getTableRow (string $ fieldName , string $ typeName ): string
9092 {
9193 if ($ this ->isJson ($ typeName )) {
9294 return $ this ->getJsonLine ($ fieldName );
@@ -100,6 +102,6 @@ protected function getTableRow($fieldName, $typeName): string
100102 return $ this ->getNonRequiredLine ($ fieldName , $ typeName );
101103 }
102104
103- throw new Exception ( ' Unknown fieldType in MigrationGenerator ' );
105+ throw new UnknownFieldTypeException ( $ typeName , ' MigrationGenerator ' );
104106 }
105107}
0 commit comments