Skip to content

Commit 4b54316

Browse files
authored
Merge pull request mavinoo#58 from WalterWoshid/master
Fixed increment/decrement bad syntax
2 parents 7eb48e6 + 656a352 commit 4b54316

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ $userInstance = new User;
8383
$value = [
8484
[
8585
'id' => 1,
86-
'balance' => '+500' // Add
86+
'balance' => ['+', 500] // Add
8787
] ,
8888
[
8989
'id' => 2,
90-
'balance' => '-200' // Subtract
90+
'balance' => ['-', 200] // Subtract
9191
] ,
9292
[
9393
'id' => 3,
94-
'balance' => '*5' // Multiply
94+
'balance' => ['*', 5] // Multiply
9595
] ,
9696
[
9797
'id' => 4,
98-
'balance' => '/2' // Divide
98+
'balance' => ['/', 2] // Divide
9999
] ,
100100
[
101101
'id' => 5,
102-
'balance' => '%2' // Modulo
102+
'balance' => ['%', 2] // Modulo
103103
] ,
104104
];
105105
$index = 'id';

src/Batch.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,37 @@ public function __construct(DatabaseManager $db)
1919
}
2020

2121
/**
22-
* Update multiple rows.
22+
* <h2>Update multiple rows.</h2>
2323
*
24-
* @param Model $table
25-
* @param array $values
26-
* @param string $index
27-
* @param bool $raw
28-
* @return bool|int
29-
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
30-
* @desc
31-
* Example
32-
* $table = 'users';
24+
* Example:<br>
25+
* ```
26+
* $userInstance = new \App\Models\User;
3327
* $value = [
3428
* [
3529
* 'id' => 1,
3630
* 'status' => 'active',
3731
* 'nickname' => 'Mohammad'
38-
* ] ,
32+
* ],
3933
* [
4034
* 'id' => 5,
4135
* 'status' => 'deactive',
4236
* 'nickname' => 'Ghanbari'
43-
* ] ,
37+
* ],
38+
* [
39+
* 'id' => 7,
40+
* 'balance' => ['+', 500]
41+
* ]
4442
* ];
4543
* $index = 'id';
44+
* Batch::update($userInstance, $value, $index);
45+
* ```
46+
*
47+
* @param \Illuminate\Database\Eloquent\Model $table
48+
* @param array $values
49+
* @param string $index
50+
* @param bool $raw
51+
* @return bool|int
52+
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
4653
*/
4754
public function update(Model $table, array $values, string $index = null, bool $raw = false)
4855
{
@@ -73,12 +80,28 @@ public function update(Model $table, array $values, string $index = null, bool $
7380

7481
foreach (array_keys($val) as $field) {
7582
if ($field !== $index) {
76-
if (gettype($val[$field]) == 'string' && !empty($val[$field]) && str_replace(['+', '-', '*', '/', '%'], '', $val[$field][0]) !== $val[$field][0]) {
77-
$value = '`' . $field . '`' . $val[$field];
83+
// If increment / decrement
84+
if (gettype($val[$field]) == 'array') {
85+
// If array has two values
86+
if (!array_key_exists(0, $val[$field]) || !array_key_exists(1, $val[$field])) {
87+
throw new \ArgumentCountError('Increment/Decrement array needs to have 2 values, a math operator (+, -, *, /, %) and a number');
88+
}
89+
// Check first value
90+
if (gettype($val[$field][0]) != 'string' || !in_array($val[$field][0], ['+', '-', '*', '/', '%'])) {
91+
throw new \TypeError('First value in Increment/Decrement array needs to be a string and a math operator (+, -, *, /, %)');
92+
}
93+
// Check second value
94+
if (!is_numeric($val[$field][1])) {
95+
throw new \TypeError('Second value in Increment/Decrement array needs to be numeric');
96+
}
97+
// Increment / decrement
98+
$value = '`' . $field . '`' . $val[$field][0] . $val[$field][1];
7899
} else {
100+
// Only update
79101
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'";
80102
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
81103
}
104+
82105
if ($driver == 'pgsql')
83106
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' ';
84107
else

0 commit comments

Comments
 (0)