Skip to content

Commit aa8ff46

Browse files
committed
added increment and decrement methods
1 parent b5323a3 commit aa8ff46

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ public function __call($method, $parameters)
326326
"keyCondition",
327327
"keyConditionIn",
328328
"keyConditionBetween",
329+
'increment',
330+
'decrement'
329331
];
330332

331333
if (! in_array($method, $allowedBuilderMethods)) {

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,40 @@ public function updateItem($item)
295295
return $this->process('updateItem', null);
296296
}
297297

298+
/**
299+
* @inheritdoc
300+
*/
301+
public function increment($column, $amount = 1, array $extra = [])
302+
{
303+
return $this->incrementOrDecrement($column, '+', $amount, $extra);
304+
}
305+
306+
/**
307+
* @inheritdoc
308+
*/
309+
public function decrement($column, $amount = 1, array $extra = [])
310+
{
311+
return $this->incrementOrDecrement($column, '-', $amount, $extra);
312+
}
313+
314+
/**
315+
* Increment or decrement column's value by a given amount.
316+
*
317+
* @param $column
318+
* @param $symbol
319+
* @param int $amount
320+
* @param array $extra
321+
* @return array|\Aws\Result|Aws\Result|Illuminate\Support\Collection
322+
*/
323+
protected function incrementOrDecrement($column, $symbol, $amount = 1, array $extra = [])
324+
{
325+
$name = $this->expression_attributes->addName($column);
326+
$value = $this->expression_attributes->addValue($amount);
327+
$this->updates['set'][] = "{$name} = {$name} {$symbol} {$value}";
328+
329+
return $this->updateItem($extra);
330+
}
331+
298332
/**
299333
* Query.
300334
*

tests/Query/BuilderTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,90 @@ public function it_can_process_update_item()
700700
$this->assertNull($query['processor']);
701701
}
702702

703+
/** @test */
704+
public function it_can_increment_value_of_attribute()
705+
{
706+
$method = 'updateItem';
707+
$params = [
708+
'TableName' => 'Thread',
709+
'Key' => [
710+
'ForumName' => [
711+
'S' => 'Laravel'
712+
],
713+
'Subject' => [
714+
'S' => 'Laravel Thread 1'
715+
]
716+
],
717+
'UpdateExpression' => 'set #1 = #1 + :1, #2 = :2',
718+
'ExpressionAttributeNames' => [
719+
'#1' => 'Replies',
720+
'#2' => 'LastPostedBy'
721+
],
722+
'ExpressionAttributeValues' => [
723+
':1' => [
724+
'N' => '2'
725+
],
726+
':2' => [
727+
'S' => 'User A'
728+
]
729+
]
730+
];
731+
732+
$query = $this->newQuery('Thread')
733+
->key([
734+
'ForumName' => 'Laravel',
735+
'Subject' => 'Laravel Thread 1'
736+
])->increment('Replies', 2, [
737+
'LastPostedBy' => 'User A'
738+
]);
739+
740+
$this->assertEquals($method, $query['method']);
741+
$this->assertEquals($params, $query['params']);
742+
$this->assertNull($query['processor']);
743+
}
744+
745+
/** @test */
746+
public function it_can_decrement_value_of_attribute()
747+
{
748+
$method = 'updateItem';
749+
$params = [
750+
'TableName' => 'Thread',
751+
'Key' => [
752+
'ForumName' => [
753+
'S' => 'Laravel'
754+
],
755+
'Subject' => [
756+
'S' => 'Laravel Thread 1'
757+
]
758+
],
759+
'UpdateExpression' => 'set #1 = #1 - :1, #2 = :2',
760+
'ExpressionAttributeNames' => [
761+
'#1' => 'Replies',
762+
'#2' => 'LastPostedBy'
763+
],
764+
'ExpressionAttributeValues' => [
765+
':1' => [
766+
'N' => '2'
767+
],
768+
':2' => [
769+
'S' => 'User A'
770+
]
771+
]
772+
];
773+
774+
$query = $this->newQuery('Thread')
775+
->key([
776+
'ForumName' => 'Laravel',
777+
'Subject' => 'Laravel Thread 1'
778+
])->decrement('Replies', 2, [
779+
'LastPostedBy' => 'User A'
780+
]);
781+
782+
$this->assertEquals($method, $query['method']);
783+
$this->assertEquals($params, $query['params']);
784+
$this->assertNull($query['processor']);
785+
}
786+
703787
/** @test */
704788
public function it_can_set_single_attribute()
705789
{

0 commit comments

Comments
 (0)