1- <?php
1+ <?php declare (strict_types= 1 );
22
33namespace Mavinoo \Batch ;
44
5- use Illuminate \Database \DatabaseManager ;
6- use Illuminate \Database \Eloquent \Model ;
75use Mavinoo \Batch \Common \Common ;
6+ use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \DatabaseManager ;
88
99class Batch implements BatchInterface
1010{
@@ -19,12 +19,14 @@ public function __construct(DatabaseManager $db)
1919 }
2020
2121 /**
22- * Update multiple rows
22+ * Update multiple rows.
23+ *
2324 * @param Model $table
2425 * @param array $values
2526 * @param string $index
27+ * @param bool $raw
28+ * @return bool|int
2629 * @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
27- *
2830 * @desc
2931 * Example
3032 * $table = 'users';
@@ -41,19 +43,17 @@ public function __construct(DatabaseManager $db)
4143 * ] ,
4244 * ];
4345 * $index = 'id';
44- *
45- * @return bool|int
4646 */
4747 public function update (Model $ table , array $ values , string $ index = null , bool $ raw = false )
4848 {
4949 $ final = [];
5050 $ ids = [];
5151
52- if (!count ($ values )) {
52+ if (! count ($ values )) {
5353 return false ;
5454 }
5555
56- if (!isset ($ index ) || empty ($ index )) {
56+ if (! isset ($ index ) || empty ($ index )) {
5757 $ index = $ table ->getKeyName ();
5858 }
5959
@@ -74,21 +74,21 @@ public function update(Model $table, array $values, string $index = null, bool $
7474 . 'ELSE ` ' . $ k . '` END), ' ;
7575 }
7676
77- $ query = " UPDATE ` " . $ this ->getFullTableName ($ table ) . " ` SET " . substr ($ cases , 0 , -2 ) . " WHERE ` $ index` IN( " . '" ' . implode ('"," ' , $ ids ) . '" ' . " ); " ;
77+ $ query = ' UPDATE ` ' . $ this ->getFullTableName ($ table ) . ' ` SET ' . substr ($ cases , 0 , -2 ) . " WHERE ` $ index` IN( " . '" ' . implode ('"," ' , $ ids ) . '" ' . ' ); ' ;
7878
7979 return $ this ->db ->connection ($ this ->getConnectionName ($ table ))->update ($ query );
8080 }
8181
8282 /**
83- * Update multiple rows
83+ * Update multiple rows.
84+ *
8485 * @param Model $table
8586 * @param array $values
8687 * @param string $index
8788 * @param string|null $index2
8889 * @param bool $raw
8990 * @return bool|int
9091 * @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
91- *
9292 * @desc
9393 * Example
9494 * $table = 'users';
@@ -106,45 +106,45 @@ public function update(Model $table, array $values, string $index = null, bool $
106106 * ];
107107 * $index = 'id';
108108 * $index2 = 'user_id';
109- *
110109 */
111110 public function updateWithTwoIndex (Model $ table , array $ values , string $ index = null , string $ index2 = null , bool $ raw = false )
112111 {
113112 $ final = [];
114113 $ ids = [];
115114
116- if (!count ($ values )) {
115+ if (! count ($ values )) {
117116 return false ;
118117 }
119118
120- if (!isset ($ index ) || empty ($ index )) {
119+ if (! isset ($ index ) || empty ($ index )) {
121120 $ index = $ table ->getKeyName ();
122121 }
123122
124123 foreach ($ values as $ key => $ val ) {
125- $ ids [] = $ val [$ index ];
124+ $ ids [] = $ val [$ index ];
126125 $ ids2 [] = $ val [$ index2 ];
127126 foreach (array_keys ($ val ) as $ field ) {
128- if ($ field !== $ index || $ field !== $ index2 ) {
127+ if ($ field !== $ index || $ field !== $ index2 ) {
129128 $ finalField = $ raw ? Common::mysql_escape ($ val [$ field ]) : '" ' . Common::mysql_escape ($ val [$ field ]) . '" ' ;
130129 $ value = (is_null ($ val [$ field ]) ? 'NULL ' : $ finalField );
131- $ final [$ field ][] = 'WHEN (` ' . $ index . '` = " ' . Common::mysql_escape ($ val [$ index ]) .'" AND ` ' . $ index2 . '` = " ' . $ val [$ index2 ] .'") THEN ' . $ value . ' ' ;
130+ $ final [$ field ][] = 'WHEN (` ' . $ index . '` = " ' . Common::mysql_escape ($ val [$ index ]) . '" AND ` ' . $ index2 . '` = " ' . $ val [$ index2 ] . '") THEN ' . $ value . ' ' ;
132131 }
133132 }
134133 }
135134
136135 $ cases = '' ;
137136 foreach ($ final as $ k => $ v ) {
138137 $ cases .= '` ' . $ k . '` = (CASE ' . implode ("\n" , $ v ) . "\n"
139- . 'ELSE ` ' . $ k . '` END), ' ;
138+ . 'ELSE ` ' . $ k . '` END), ' ;
140139 }
141- $ query = " UPDATE ` " . $ this ->getFullTableName ($ table ) . " ` SET " . substr ($ cases , 0 , -2 ) . " WHERE ` $ index` IN( " . '" ' . implode ('"," ' , $ ids ) . '") ' . " AND ` $ index2` IN( " . '" ' . implode ('"," ' , $ ids2 ) . '" ' ." ); " ;
140+ $ query = ' UPDATE ` ' . $ this ->getFullTableName ($ table ) . ' ` SET ' . substr ($ cases , 0 , -2 ) . " WHERE ` $ index` IN( " . '" ' . implode ('"," ' , $ ids ) . '") ' . " AND ` $ index2` IN( " . '" ' . implode ('"," ' , $ ids2 ) . '" ' . ' ); ' ;
142141
143142 return $ this ->db ->connection ($ this ->getConnectionName ($ table ))->update ($ query );
144143 }
145144
146145 /**
147- * Insert Multi rows
146+ * Insert Multi rows.
147+ *
148148 * @param Model $table
149149 * @param array $columns
150150 * @param array $values
@@ -153,7 +153,6 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
153153 * @return bool|mixed
154154 * @throws \Throwable
155155 * @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
156- *
157156 * @desc
158157 * Example
159158 *
@@ -194,7 +193,7 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
194193 {
195194 // no need for the old validation since we now use type hint that supports from php 7.0
196195 // but I kept this one
197- if (count ($ columns ) != count ($ values [0 ])) {
196+ if (count ($ columns ) !== count ($ values [0 ])) {
198197 return false ;
199198 }
200199
@@ -209,7 +208,7 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
209208 $ values = array_chunk ($ values , $ totalChunk , true );
210209
211210 foreach ($ columns as $ key => $ column ) {
212- $ columns [$ key ] = " ` " . Common::mysql_escape ($ column ) . " ` " ;
211+ $ columns [$ key ] = ' ` ' . Common::mysql_escape ($ column ) . ' ` ' ;
213212 }
214213
215214 foreach ($ values as $ value ) {
@@ -225,14 +224,13 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
225224
226225 $ valueString = implode (', ' , $ valueArray );
227226
228- $ ignoreStmt = $ insertIgnore ? ' IGNORE ' : '' ;
227+ $ ignoreStmt = $ insertIgnore ? ' IGNORE ' : '' ;
229228
230- $ query [] = " INSERT " . $ ignoreStmt. " INTO `" . $ this ->getFullTableName ($ table ) . " ` ( " . implode (', ' , $ columns ) . ") VALUES $ valueString; " ;
229+ $ query [] = ' INSERT ' . $ ignoreStmt . ' INTO `' . $ this ->getFullTableName ($ table ) . ' ` ( ' . implode (', ' , $ columns ) . ") VALUES $ valueString; " ;
231230 }
232231
233232 if (count ($ query )) {
234233 return $ this ->db ->transaction (function () use ($ totalValues , $ totalChunk , $ query , $ table ) {
235-
236234 $ totalQuery = 0 ;
237235 foreach ($ query as $ value ) {
238236 $ totalQuery += $ this ->db ->connection ($ this ->getConnectionName ($ table ))->statement ($ value ) ? 1 : 0 ;
@@ -250,6 +248,8 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
250248 }
251249
252250 /**
251+ * Get full table name.
252+ *
253253 * @param Model $model
254254 * @return string
255255 * @author Ibrahim Sakr <ebrahimes@gmail.com>
@@ -260,14 +260,17 @@ private function getFullTableName(Model $model)
260260 }
261261
262262 /**
263+ * Get connection name.
264+ *
263265 * @param Model $model
264266 * @return string|null
265267 * @author Ibrahim Sakr <ebrahimes@gmail.com>
266268 */
267269 private function getConnectionName (Model $ model )
268270 {
269- if (!is_null ($ cn = $ model ->getConnectionName ()))
271+ if (! is_null ($ cn = $ model ->getConnectionName ())) {
270272 return $ cn ;
273+ }
271274
272275 return $ model ->getConnection ()->getName ();
273276 }
0 commit comments