Skip to content

Commit fcf36ef

Browse files
committed
Increment and decrement implementation
1 parent 9d8b0d4 commit fcf36ef

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/KVOption.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function exists($key)
4646
* Get the specified option by key.
4747
*
4848
* @param string $key
49-
* @param mixed $default
49+
* @param mixed $default
5050
* @return mixed
5151
*/
5252
public function get($key, $default = null)
@@ -62,7 +62,7 @@ public function get($key, $default = null)
6262
* Set a given option.
6363
*
6464
* @param string $key
65-
* @param string $comment
65+
* @param string $comment
6666
* @param mixed $value
6767
* @return void
6868
*/
@@ -116,6 +116,48 @@ public function setArray($array)
116116

117117
}
118118

119+
/**
120+
* Increment a value from the options.
121+
*
122+
* @param string $key
123+
* @param int $factor
124+
*
125+
* @return int|null|string
126+
*/
127+
public function increment($key, int $factor = 1)
128+
{
129+
$currentValue = $this->get($key) ?? 0;
130+
$newValue = $currentValue + $factor;
131+
132+
$this->updateOrCreate(
133+
['key' => $key],
134+
['value' => $newValue]
135+
);
136+
137+
return $newValue;
138+
}
139+
140+
/**
141+
* Decrement a value from the options.
142+
*
143+
* @param string $key
144+
* @param int $factor
145+
*
146+
* @return int|null|string
147+
*/
148+
public function decrement($key, int $factor = 1)
149+
{
150+
$currentValue = $this->get($key) ?? 0;
151+
$newValue = $currentValue - $factor;
152+
153+
$this->updateOrCreate(
154+
['key' => $key],
155+
['value' => $newValue]
156+
);
157+
158+
return $newValue;
159+
}
160+
119161
/**
120162
* Delete the specified option.
121163
*

src/KVOptionJSON.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function exists($key) : bool
3333
* Get the specified option by key.
3434
*
3535
* @param string $key
36-
* @param mixed $default
36+
* @param mixed $default
3737
* @return mixed
3838
*/
3939
public function get($key, $default = null)
@@ -49,7 +49,7 @@ public function get($key, $default = null)
4949
* Set a given option.
5050
*
5151
* @param string $key
52-
* @param string $comment
52+
* @param string $comment
5353
* @param mixed $value
5454
* @return void
5555
*
@@ -81,6 +81,42 @@ public function setArray($array)
8181
$this->setContent();
8282
}
8383

84+
/**
85+
* Increment a value from the options.
86+
*
87+
* @param string $key
88+
* @param int $factor
89+
*
90+
* @return int|null|string
91+
*/
92+
public function increment($key, int $factor = 1)
93+
{
94+
$currentValue = $this->get($key) ?? 0;
95+
$newValue = $currentValue + $factor;
96+
97+
$this->set($key, $newValue);
98+
99+
return $newValue;
100+
}
101+
102+
/**
103+
* Decrement a value from the options.
104+
*
105+
* @param string $key
106+
* @param int $factor
107+
*
108+
* @return int|null|string
109+
*/
110+
public function decrement($key, int $factor = 1)
111+
{
112+
$currentValue = $this->get($key) ?? 0;
113+
$newValue = $currentValue - $factor;
114+
115+
$this->set($key, $newValue);
116+
117+
return $newValue;
118+
}
119+
84120
/**
85121
* Delete the specified option.
86122
*

0 commit comments

Comments
 (0)