22
33namespace Eghamat24 \DatabaseRepository \Creators ;
44
5+ use Eghamat24 \DatabaseRepository \Models \Enums \DataTypeEnum ;
56use Illuminate \Support \Collection ;
67use Illuminate \Support \Str ;
78use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
@@ -38,8 +39,8 @@ public function createUses(): array
3839 return [
3940 "use $ this ->entityNamespace \\$ this ->entityName ; " ,
4041 "use $ this ->factoryNamespace \\$ this ->factoryName ; " ,
41- " use Illuminate\Support\Collection; " ,
42- " use Eghamat24\DatabaseRepository\Models\Repositories\MySqlRepository; "
42+ ' use Illuminate\Support\Collection; ' ,
43+ ' use Eghamat24\DatabaseRepository\Models\Repositories\MySqlRepository; '
4344 ];
4445 }
4546
@@ -50,7 +51,7 @@ public function getClassName(): string
5051
5152 public function getExtendSection (): string
5253 {
53- return " extends MySqlRepository implements " . $ this ->interfaceName ;
54+ return ' extends MySqlRepository implements ' . $ this ->interfaceName ;
5455 }
5556
5657 public function createAttributes (): array
@@ -61,96 +62,80 @@ public function createAttributes(): array
6162 public function createFunctions (): array
6263 {
6364
64- $ baseContent = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'class.stub ' );
65- $ constructContent = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'construct.stub ' );
66- $ getOneStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'getOneBy.stub ' );
67- $ getAllStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'getAllBy.stub ' );
68- $ createFunctionStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'create.stub ' );
69- $ updateFunctionStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'update.stub ' );
70- $ deleteStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'delete.stub ' );
71- $ undeleteStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'undelete.stub ' );
72- $ getterStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'getter.stub ' );
73- $ setterStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'setter.stub ' );
74- $ timeFieldStub = file_get_contents ($ this ->mysqlRepositoryStubsPath . 'timeField.stub ' );
65+ $ stubList = [
66+ 'baseContent ' => 'class.stub ' ,
67+ 'constructContent ' => 'construct.stub ' ,
68+ 'getOneStub ' => 'getOneBy.stub ' ,
69+ 'getAllStub ' => 'getAllBy.stub ' ,
70+ 'createFunctionStub ' => 'create.stub ' ,
71+ 'updateFunctionStub ' => 'update.stub ' ,
72+ 'deleteStub ' => 'delete.stub ' ,
73+ 'undeleteStub ' => 'undelete.stub ' ,
74+ 'getterStub ' => 'getter.stub ' ,
75+ 'setterStub ' => 'setter.stub ' ,
76+ 'timeFieldStub ' => 'timeField.stub ' ,
77+ ];
78+
79+ $ stubContent = [];
80+ foreach ($ stubList as $ stubKey => $ stubName ) {
81+ $ stubContent [$ stubKey ] = file_get_contents ($ this ->mysqlRepositoryStubsPath . $ stubName );
82+ }
7583
76- $ functions = [];
77- // Initialize MySql Repository
7884 $ hasSoftDelete = in_array ('deleted_at ' , $ this ->columns ->pluck ('COLUMN_NAME ' )->toArray (), true );
79- $ functions ['__construct ' ] = $ this ->getConstruct ($ this ->tableName , $ this ->factoryName , $ hasSoftDelete , $ constructContent );
80- $ functions ['getOneById ' ] = $ this ->writeGetOneFunction ($ getOneStub , 'id ' , 'int ' );
81- $ functions ['getAllByIds ' ] = $ this ->writeGetAllFunction ($ getAllStub , 'id ' , 'int ' );
85+
86+ $ functions = [];
87+ $ functions ['__construct ' ] = $ this ->getConstruct ($ this ->tableName , $ this ->factoryName , $ hasSoftDelete , $ stubContent ['constructContent ' ]);
88+ $ functions ['getOneById ' ] = $ this ->writeGetOneFunction ($ stubContent ['getOneStub ' ], 'id ' , DataTypeEnum::INTEGER_TYPE );
89+ $ functions ['getAllByIds ' ] = $ this ->writeGetAllFunction ($ stubContent ['getAllStub ' ], 'id ' , DataTypeEnum::INTEGER_TYPE );
8290 $ columnsInfo = $ this ->getAllColumnsInTable ($ this ->tableName );
8391
8492 $ indexes = $ this ->extractIndexes ($ this ->tableName );
8593 foreach ($ indexes as $ index ) {
8694 $ columnInfo = collect ($ columnsInfo )->where ('COLUMN_NAME ' , $ index ->COLUMN_NAME )->first ();
8795 $ indx = 'getOneBy ' . ucfirst (Str::camel ($ index ->COLUMN_NAME ));
88- $ functions [$ indx ] = $ this ->writeGetOneFunction ($ getOneStub , $ index ->COLUMN_NAME , $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE ));
96+ $ functions [$ indx ] = $ this ->writeGetOneFunction (
97+ $ stubContent ['getOneStub ' ],
98+ $ index ->COLUMN_NAME ,
99+ $ this ->getDataType ($ columnInfo ->COLUMN_TYPE , $ columnInfo ->DATA_TYPE )
100+ );
89101
90102 if ($ index ->Non_unique == 1 ) {
91103 $ indx = 'getAllBy ' . ucfirst (Str::plural (Str::camel ($ index ->COLUMN_NAME )));
92- $ functions [$ indx ] = $ this ->writeGetAllFunction ($ getAllStub , $ index ->COLUMN_NAME , $ this ->entityName );
104+ $ functions [$ indx ] = $ this ->writeGetAllFunction ($ stubContent [ ' getAllStub ' ] , $ index ->COLUMN_NAME , $ this ->entityName );
93105 }
94106 }
95107
96108 if ($ this ->detectForeignKeys ) {
97109 $ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
98110 foreach ($ foreignKeys as $ _foreignKey ) {
99111 $ indx = 'getOneBy ' . ucfirst (Str::camel ($ _foreignKey ->COLUMN_NAME ));
100- $ functions [$ indx ] = $ this ->writeGetOneFunction ($ getOneStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
112+ $ functions [$ indx ] = $ this ->writeGetOneFunction ($ stubContent [ ' getOneStub ' ] , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
101113 $ indx = 'getAllBy ' . ucfirst (Str::plural (Str::camel ($ _foreignKey ->COLUMN_NAME )));
102- $ functions [$ indx ] = $ this ->writeGetAllFunction ($ getAllStub , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
114+ $ functions [$ indx ] = $ this ->writeGetAllFunction ($ stubContent [ ' getAllStub ' ] , $ _foreignKey ->COLUMN_NAME , $ this ->entityName );
103115 }
104116 }
105117
106118 $ getterFunctions = '' ;
107119 $ setterFunctions = '' ;
108- // Create "create" function
109- foreach ($ this ->columns as $ _column ) {
110- if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'deleted_at ' ])) {
111- $ getterFunctions .= trim ($ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
112- }
113- if (in_array ($ _column ->COLUMN_NAME , ['created_at ' , 'updated_at ' ], true )) {
114- $ setterFunctions .= trim ($ this ->writeSetterFunction ($ setterStub , $ _column ->COLUMN_NAME )) . "\n\t\t" ;
115- }
116- }
117- $ createFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ SetterFunctions }} " ],
118- [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
119- $ createFunctionStub
120- );
121-
122- $ functions ['create ' ] = $ createFunctionStub ;
120+ $ functions = $ this ->makeCreateFunction ($ stubContent , $ getterFunctions , $ setterFunctions , $ functions );
123121
124122 $ getterFunctions = '' ;
125123 $ setterFunctions = '' ;
126- // Create "update" function
127- foreach ($ this ->columns as $ _column ) {
128- if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'deleted_at ' ])) {
129- $ getterFunctions .= trim ($ this ->writeGetterFunction ($ getterStub , $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
130- }
131- if ($ _column ->COLUMN_NAME === 'updated_at ' ) {
132- $ setterFunctions .= trim ($ this ->writeSetterFunction ($ setterStub , $ _column ->COLUMN_NAME )) . "\n\t\t" ;
133- }
134- }
135- $ updateFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ UpdateFieldSetter }} " ],
136- [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
137- $ updateFunctionStub
138- );
139-
140- $ functions ['update ' ] = $ updateFunctionStub ;
124+ $ functions = $ this ->makeUpdateFunction ($ stubContent , $ getterFunctions , $ setterFunctions , $ functions );
141125
142126 // Create "delete" and "undelete" functions if necessary
143127 if ($ hasSoftDelete ) {
144- $ functions ['remove ' ] = $ deleteStub ;
145- $ functions ['restore ' ] = $ undeleteStub ;
128+ $ functions ['remove ' ] = $ stubContent [ ' deleteStub ' ] ;
129+ $ functions ['restore ' ] = $ stubContent [ ' undeleteStub ' ] ;
146130 }
147131
148132 foreach ($ functions as &$ func ) {
149- $ func = str_replace ([" {{ EntityName }} " , " {{ EntityVariableName }} " ],
133+ $ func = str_replace ([' {{ EntityName }} ' , ' {{ EntityVariableName }} ' ],
150134 [$ this ->entityName , $ this ->entityVariableName ],
151135 $ func
152136 );
153137 }
138+
154139 return $ functions ;
155140 }
156141
@@ -189,4 +174,63 @@ private function getConstruct(string $tableName, string $factoryName, bool $hasS
189174 [$ tableName , $ factoryName , $ hasSoftDelete ? 'true ' : 'false ' ],
190175 $ constructContent );
191176 }
177+
178+ /**
179+ * @param array $stubContent
180+ * @param string $getterFunctions
181+ * @param string $setterFunctions
182+ * @param array $functions
183+ * @return array
184+ */
185+ public function makeCreateFunction (array &$ stubContent , string &$ getterFunctions , string &$ setterFunctions , array &$ functions ): array
186+ {
187+ foreach ($ this ->columns as $ _column ) {
188+ if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'deleted_at ' ])) {
189+ $ getterFunctions .= trim ($ this ->writeGetterFunction ($ stubContent ['getterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
190+ }
191+
192+ if (in_array ($ _column ->COLUMN_NAME , ['created_at ' , 'updated_at ' ], true )) {
193+ $ setterFunctions .= trim ($ this ->writeSetterFunction ($ stubContent ['setterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t" ;
194+ }
195+ }
196+
197+ $ createFunctionStub = str_replace (["{{ GetterFunctions }} " , "{{ SetterFunctions }} " ],
198+ [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
199+ $ stubContent ['createFunctionStub ' ]
200+ );
201+
202+ $ functions ['create ' ] = $ createFunctionStub ;
203+
204+ return $ functions ;
205+ }
206+
207+ /**
208+ * @param array $stubContent
209+ * @param string $getterFunctions
210+ * @param string $setterFunctions
211+ * @param array $functions
212+ * @return array
213+ */
214+ public function makeUpdateFunction (array &$ stubContent , string &$ getterFunctions , string &$ setterFunctions , array &$ functions ): array
215+ {
216+ foreach ($ this ->columns as $ _column ) {
217+
218+ if (!in_array ($ _column ->COLUMN_NAME , ['id ' , 'created_at ' , 'deleted_at ' ])) {
219+ $ getterFunctions .= trim ($ this ->writeGetterFunction ($ stubContent ['getterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t\t\t" ;
220+ }
221+
222+ if ($ _column ->COLUMN_NAME === 'updated_at ' ) {
223+ $ setterFunctions .= trim ($ this ->writeSetterFunction ($ stubContent ['setterStub ' ], $ _column ->COLUMN_NAME )) . "\n\t\t" ;
224+ }
225+ }
226+
227+ $ updateFunctionStub = str_replace (['{{ GetterFunctions }} ' , '{{ UpdateFieldSetter }} ' ],
228+ [trim (substr ($ getterFunctions , 0 , -1 )), trim (substr ($ setterFunctions , 0 , -1 ))],
229+ $ stubContent ['updateFunctionStub ' ]
230+ );
231+
232+ $ functions ['update ' ] = $ updateFunctionStub ;
233+
234+ return $ functions ;
235+ }
192236}
0 commit comments