Skip to content

Commit ebf0d16

Browse files
authored
Merge pull request #99 from baopham/v1-remove-nested-attributes
Support remove nested attributes
2 parents f9c2e57 + 18aec12 commit ebf0d16

File tree

5 files changed

+108
-40
lines changed

5 files changed

+108
-40
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ $foo->withoutGlobalScopes()->countUnder(6)->get();
246246
247247
```php
248248
$model = new Model();
249-
$model->where('id', 'foo')->removeAttribute('name', 'description');
249+
$model->where('id', 'foo')->removeAttribute('name', 'description', 'nested.foo', 'nestedArray[0]');
250250
251251
// Or
252-
Model::find('foo')->removeAttribute('name', 'description');
252+
Model::find('foo')->removeAttribute('name', 'description', 'nested.foo', 'nestedArray[0]');
253253
```
254254
255255
Indexes

src/Parsers/ExpressionAttributeNames.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class ExpressionAttributeNames
99
*/
1010
protected $mapping;
1111

12+
/**
13+
* @var array
14+
*/
15+
protected $nested;
16+
1217
/**
1318
* @var string
1419
*/
@@ -22,6 +27,10 @@ public function __construct($prefix = '#')
2227

2328
public function set($name)
2429
{
30+
if ($this->isNested($name)) {
31+
$this->nested[] = $name;
32+
return;
33+
}
2534
$this->mapping["{$this->prefix}{$name}"] = $name;
2635
}
2736

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

3847
public function placeholders()
3948
{
40-
return array_keys($this->mapping);
49+
return array_merge(array_keys($this->mapping), $this->nested);
4150
}
4251

4352
public function reset()
4453
{
4554
$this->mapping = [];
55+
$this->nested = [];
4656

4757
return $this;
4858
}
59+
60+
private function isNested($name)
61+
{
62+
return strpos($name, '.') !== false || strpos($name, '[') !== false;
63+
}
4964
}

tests/DynamoDbCompositeModelTest.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,47 +323,35 @@ public function testConditionsNotContainingAllCompositeKeys()
323323
$this->assertEquals($expectedItem, $foundItems->first()->toArray());
324324
}
325325

326-
public function testRemoveUpdateExpressionOnQuery()
326+
public function testRemoveAttributeOnQuery()
327327
{
328-
$seed = $this->seed([
328+
$this->seed([
329329
'id' => ['S' => 'foo'],
330330
'id2' => ['S' => 'bar']
331331
]);
332332

333-
$this->assertNotNull(array_get($seed, 'name.S'));
334-
$this->assertNotNull(array_get($seed, 'description.S'));
335-
336333
$this->testModel
337334
->where('id', 'foo')
338335
->where('id2', 'bar')
339-
->removeAttribute('description', 'name');
336+
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
340337

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

343-
$this->assertNull($item->name);
344-
$this->assertNull($item->description);
345-
$this->assertNotNull($item->count);
346-
$this->assertNotNull($item->author);
340+
$this->assertRemoveAttribute($item);
347341
}
348342

349-
public function testRemoveUpdateExpressionOnModel()
343+
public function testRemoveAttributeOnModel()
350344
{
351-
$seed = $this->seed([
345+
$this->seed([
352346
'id' => ['S' => 'foo'],
353347
'id2' => ['S' => 'bar']
354348
]);
355349

356-
$this->assertNotNull(array_get($seed, 'name.S'));
357-
$this->assertNotNull(array_get($seed, 'description.S'));
358-
359350
$item = $this->testModel->first();
360-
$item->removeAttribute('description', 'name');
351+
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
361352
$item = $this->testModel->first();
362353

363-
$this->assertNull($item->name);
364-
$this->assertNull($item->description);
365-
$this->assertNotNull($item->count);
366-
$this->assertNotNull($item->author);
354+
$this->assertRemoveAttribute($item);
367355
}
368356

369357
protected function seed($attributes = [], $exclude = [])
@@ -375,6 +363,19 @@ protected function seed($attributes = [], $exclude = [])
375363
'description' => ['S' => str_random(256)],
376364
'count' => ['N' => rand()],
377365
'author' => ['S' => str_random()],
366+
'nested' => [
367+
'M' => [
368+
'foo' => ['S' => 'bar'],
369+
'nestedArray' => ['L' => [['S' => 'first']]],
370+
'hello' => ['S' => 'world'],
371+
],
372+
],
373+
'nestedArray' => [
374+
'L' => [
375+
['S' => 'first'],
376+
['S' => 'second'],
377+
],
378+
],
378379
];
379380

380381
$item = array_merge($item, $attributes);

tests/DynamoDbModelTest.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -658,36 +658,38 @@ public function testLimit()
658658
$this->assertEquals(4, $items->count());
659659
}
660660

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

665-
$this->assertNotNull(array_get($seed, 'name.S'));
666-
$this->assertNotNull(array_get($seed, 'description.S'));
667-
668-
$this->testModel->where('id', 'foo')->removeAttribute('description', 'name');
665+
$this->testModel
666+
->where('id', 'foo')
667+
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
669668

670669
$item = $this->testModel->find('foo');
671-
672-
$this->assertNull($item->name);
673-
$this->assertNull($item->description);
674-
$this->assertNotNull($item->count);
675-
$this->assertNotNull($item->author);
670+
$this->assertRemoveAttribute($item);
676671
}
677672

678-
public function testRemoveUpdateExpressionOnModel()
673+
public function testRemoveAttributeOnModel()
679674
{
680-
$seed = $this->seed(['id' => ['S' => 'foo']]);
681-
682-
$this->assertNotNull(array_get($seed, 'name.S'));
683-
$this->assertNotNull(array_get($seed, 'description.S'));
675+
$this->seed(['id' => ['S' => 'foo']]);
684676

685677
$item = $this->testModel->first();
686-
$item->removeAttribute('description', 'name');
678+
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
687679
$item = $this->testModel->first();
688680

681+
$this->assertRemoveAttribute($item);
682+
}
683+
684+
protected function assertRemoveAttribute($item)
685+
{
689686
$this->assertNull($item->name);
690687
$this->assertNull($item->description);
688+
$this->assertArrayNotHasKey('foo', $item->nested);
689+
$this->assertCount(0, $item->nested['nestedArray']);
690+
$this->assertCount(1, $item->nestedArray);
691+
$this->assertNotContains('first', $item->nestedArray);
692+
$this->assertNotNull($item->nested['hello']);
691693
$this->assertNotNull($item->count);
692694
$this->assertNotNull($item->author);
693695
}
@@ -700,6 +702,19 @@ protected function seed($attributes = [], $exclude = [])
700702
'description' => ['S' => str_random(256)],
701703
'count' => ['N' => rand()],
702704
'author' => ['S' => str_random()],
705+
'nested' => [
706+
'M' => [
707+
'foo' => ['S' => 'bar'],
708+
'nestedArray' => ['L' => [['S' => 'first']]],
709+
'hello' => ['S' => 'world'],
710+
],
711+
],
712+
'nestedArray' => [
713+
'L' => [
714+
['S' => 'first'],
715+
['S' => 'second'],
716+
],
717+
],
703718
];
704719

705720
$item = array_merge($item, $attributes);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace BaoPham\DynamoDb\Tests\Parsers;
4+
5+
use BaoPham\DynamoDb\Parsers\ExpressionAttributeNames;
6+
use BaoPham\DynamoDb\Parsers\UpdateExpression;
7+
use BaoPham\DynamoDb\Tests\TestCase;
8+
9+
class UpdateExpressionTest extends TestCase
10+
{
11+
/**
12+
* @var UpdateExpression
13+
*/
14+
private $parser;
15+
16+
/**
17+
* @var ExpressionAttributeNames
18+
*/
19+
private $names;
20+
21+
public function setUp()
22+
{
23+
parent::setUp();
24+
$this->names = new ExpressionAttributeNames();
25+
$this->parser = new UpdateExpression($this->names);
26+
}
27+
28+
public function testParse()
29+
{
30+
$expression = $this->parser->remove(['foo.bar', 'a', 'b', 'hello[0]']);
31+
$this->assertEquals('REMOVE #a, #b, foo.bar, hello[0]', $expression);
32+
$this->assertEquals([
33+
'#a' => 'a',
34+
'#b' => 'b'
35+
], $this->names->all());
36+
}
37+
}

0 commit comments

Comments
 (0)