Skip to content

Commit

Permalink
Merge pull request #99 from baopham/v1-remove-nested-attributes
Browse files Browse the repository at this point in the history
Support remove nested attributes
  • Loading branch information
baopham authored Oct 19, 2017
2 parents f9c2e57 + 18aec12 commit ebf0d16
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ $foo->withoutGlobalScopes()->countUnder(6)->get();
```php
$model = new Model();
$model->where('id', 'foo')->removeAttribute('name', 'description');
$model->where('id', 'foo')->removeAttribute('name', 'description', 'nested.foo', 'nestedArray[0]');
// Or
Model::find('foo')->removeAttribute('name', 'description');
Model::find('foo')->removeAttribute('name', 'description', 'nested.foo', 'nestedArray[0]');
```
Indexes
Expand Down
17 changes: 16 additions & 1 deletion src/Parsers/ExpressionAttributeNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class ExpressionAttributeNames
*/
protected $mapping;

/**
* @var array
*/
protected $nested;

/**
* @var string
*/
Expand All @@ -22,6 +27,10 @@ public function __construct($prefix = '#')

public function set($name)
{
if ($this->isNested($name)) {
$this->nested[] = $name;
return;
}
$this->mapping["{$this->prefix}{$name}"] = $name;
}

Expand All @@ -37,13 +46,19 @@ public function all()

public function placeholders()
{
return array_keys($this->mapping);
return array_merge(array_keys($this->mapping), $this->nested);
}

public function reset()
{
$this->mapping = [];
$this->nested = [];

return $this;
}

private function isNested($name)
{
return strpos($name, '.') !== false || strpos($name, '[') !== false;
}
}
41 changes: 21 additions & 20 deletions tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,47 +323,35 @@ public function testConditionsNotContainingAllCompositeKeys()
$this->assertEquals($expectedItem, $foundItems->first()->toArray());
}

public function testRemoveUpdateExpressionOnQuery()
public function testRemoveAttributeOnQuery()
{
$seed = $this->seed([
$this->seed([
'id' => ['S' => 'foo'],
'id2' => ['S' => 'bar']
]);

$this->assertNotNull(array_get($seed, 'name.S'));
$this->assertNotNull(array_get($seed, 'description.S'));

$this->testModel
->where('id', 'foo')
->where('id2', 'bar')
->removeAttribute('description', 'name');
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');

$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);

$this->assertNull($item->name);
$this->assertNull($item->description);
$this->assertNotNull($item->count);
$this->assertNotNull($item->author);
$this->assertRemoveAttribute($item);
}

public function testRemoveUpdateExpressionOnModel()
public function testRemoveAttributeOnModel()
{
$seed = $this->seed([
$this->seed([
'id' => ['S' => 'foo'],
'id2' => ['S' => 'bar']
]);

$this->assertNotNull(array_get($seed, 'name.S'));
$this->assertNotNull(array_get($seed, 'description.S'));

$item = $this->testModel->first();
$item->removeAttribute('description', 'name');
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
$item = $this->testModel->first();

$this->assertNull($item->name);
$this->assertNull($item->description);
$this->assertNotNull($item->count);
$this->assertNotNull($item->author);
$this->assertRemoveAttribute($item);
}

protected function seed($attributes = [], $exclude = [])
Expand All @@ -375,6 +363,19 @@ protected function seed($attributes = [], $exclude = [])
'description' => ['S' => str_random(256)],
'count' => ['N' => rand()],
'author' => ['S' => str_random()],
'nested' => [
'M' => [
'foo' => ['S' => 'bar'],
'nestedArray' => ['L' => [['S' => 'first']]],
'hello' => ['S' => 'world'],
],
],
'nestedArray' => [
'L' => [
['S' => 'first'],
['S' => 'second'],
],
],
];

$item = array_merge($item, $attributes);
Expand Down
49 changes: 32 additions & 17 deletions tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,36 +658,38 @@ public function testLimit()
$this->assertEquals(4, $items->count());
}

public function testRemoveUpdateExpressionOnQuery()
public function testRemoveAttributeOnQuery()
{
$seed = $this->seed(['id' => ['S' => 'foo']]);
$this->seed(['id' => ['S' => 'foo']]);

$this->assertNotNull(array_get($seed, 'name.S'));
$this->assertNotNull(array_get($seed, 'description.S'));

$this->testModel->where('id', 'foo')->removeAttribute('description', 'name');
$this->testModel
->where('id', 'foo')
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');

$item = $this->testModel->find('foo');

$this->assertNull($item->name);
$this->assertNull($item->description);
$this->assertNotNull($item->count);
$this->assertNotNull($item->author);
$this->assertRemoveAttribute($item);
}

public function testRemoveUpdateExpressionOnModel()
public function testRemoveAttributeOnModel()
{
$seed = $this->seed(['id' => ['S' => 'foo']]);

$this->assertNotNull(array_get($seed, 'name.S'));
$this->assertNotNull(array_get($seed, 'description.S'));
$this->seed(['id' => ['S' => 'foo']]);

$item = $this->testModel->first();
$item->removeAttribute('description', 'name');
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
$item = $this->testModel->first();

$this->assertRemoveAttribute($item);
}

protected function assertRemoveAttribute($item)
{
$this->assertNull($item->name);
$this->assertNull($item->description);
$this->assertArrayNotHasKey('foo', $item->nested);
$this->assertCount(0, $item->nested['nestedArray']);
$this->assertCount(1, $item->nestedArray);
$this->assertNotContains('first', $item->nestedArray);
$this->assertNotNull($item->nested['hello']);
$this->assertNotNull($item->count);
$this->assertNotNull($item->author);
}
Expand All @@ -700,6 +702,19 @@ protected function seed($attributes = [], $exclude = [])
'description' => ['S' => str_random(256)],
'count' => ['N' => rand()],
'author' => ['S' => str_random()],
'nested' => [
'M' => [
'foo' => ['S' => 'bar'],
'nestedArray' => ['L' => [['S' => 'first']]],
'hello' => ['S' => 'world'],
],
],
'nestedArray' => [
'L' => [
['S' => 'first'],
['S' => 'second'],
],
],
];

$item = array_merge($item, $attributes);
Expand Down
37 changes: 37 additions & 0 deletions tests/Parsers/UpdateExpressionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace BaoPham\DynamoDb\Tests\Parsers;

use BaoPham\DynamoDb\Parsers\ExpressionAttributeNames;
use BaoPham\DynamoDb\Parsers\UpdateExpression;
use BaoPham\DynamoDb\Tests\TestCase;

class UpdateExpressionTest extends TestCase
{
/**
* @var UpdateExpression
*/
private $parser;

/**
* @var ExpressionAttributeNames
*/
private $names;

public function setUp()
{
parent::setUp();
$this->names = new ExpressionAttributeNames();
$this->parser = new UpdateExpression($this->names);
}

public function testParse()
{
$expression = $this->parser->remove(['foo.bar', 'a', 'b', 'hello[0]']);
$this->assertEquals('REMOVE #a, #b, foo.bar, hello[0]', $expression);
$this->assertEquals([
'#a' => 'a',
'#b' => 'b'
], $this->names->all());
}
}

0 comments on commit ebf0d16

Please sign in to comment.