Skip to content

Commit

Permalink
Merge pull request #58 from WalterWoshid/master
Browse files Browse the repository at this point in the history
Fixed increment/decrement bad syntax
  • Loading branch information
mavinoo authored Mar 5, 2021
2 parents 7eb48e6 + 656a352 commit 4b54316
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ $userInstance = new User;
$value = [
[
'id' => 1,
'balance' => '+500' // Add
'balance' => ['+', 500] // Add
] ,
[
'id' => 2,
'balance' => '-200' // Subtract
'balance' => ['-', 200] // Subtract
] ,
[
'id' => 3,
'balance' => '*5' // Multiply
'balance' => ['*', 5] // Multiply
] ,
[
'id' => 4,
'balance' => '/2' // Divide
'balance' => ['/', 2] // Divide
] ,
[
'id' => 5,
'balance' => '%2' // Modulo
'balance' => ['%', 2] // Modulo
] ,
];
$index = 'id';
Expand Down
51 changes: 37 additions & 14 deletions src/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,37 @@ public function __construct(DatabaseManager $db)
}

/**
* Update multiple rows.
* <h2>Update multiple rows.</h2>
*
* @param Model $table
* @param array $values
* @param string $index
* @param bool $raw
* @return bool|int
* @updatedBy Ibrahim Sakr <[email protected]>
* @desc
* Example
* $table = 'users';
* Example:<br>
* ```
* $userInstance = new \App\Models\User;
* $value = [
* [
* 'id' => 1,
* 'status' => 'active',
* 'nickname' => 'Mohammad'
* ] ,
* ],
* [
* 'id' => 5,
* 'status' => 'deactive',
* 'nickname' => 'Ghanbari'
* ] ,
* ],
* [
* 'id' => 7,
* 'balance' => ['+', 500]
* ]
* ];
* $index = 'id';
* Batch::update($userInstance, $value, $index);
* ```
*
* @param \Illuminate\Database\Eloquent\Model $table
* @param array $values
* @param string $index
* @param bool $raw
* @return bool|int
* @updatedBy Ibrahim Sakr <[email protected]>
*/
public function update(Model $table, array $values, string $index = null, bool $raw = false)
{
Expand Down Expand Up @@ -73,12 +80,28 @@ public function update(Model $table, array $values, string $index = null, bool $

foreach (array_keys($val) as $field) {
if ($field !== $index) {
if (gettype($val[$field]) == 'string' && !empty($val[$field]) && str_replace(['+', '-', '*', '/', '%'], '', $val[$field][0]) !== $val[$field][0]) {
$value = '`' . $field . '`' . $val[$field];
// If increment / decrement
if (gettype($val[$field]) == 'array') {
// If array has two values
if (!array_key_exists(0, $val[$field]) || !array_key_exists(1, $val[$field])) {
throw new \ArgumentCountError('Increment/Decrement array needs to have 2 values, a math operator (+, -, *, /, %) and a number');
}
// Check first value
if (gettype($val[$field][0]) != 'string' || !in_array($val[$field][0], ['+', '-', '*', '/', '%'])) {
throw new \TypeError('First value in Increment/Decrement array needs to be a string and a math operator (+, -, *, /, %)');
}
// Check second value
if (!is_numeric($val[$field][1])) {
throw new \TypeError('Second value in Increment/Decrement array needs to be numeric');
}
// Increment / decrement
$value = '`' . $field . '`' . $val[$field][0] . $val[$field][1];
} else {
// Only update
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
}

if ($driver == 'pgsql')
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
else
Expand Down

0 comments on commit 4b54316

Please sign in to comment.