Skip to content

Commit 9159d94

Browse files
committed
Merge pull request #138 from clue-labs/remove-attribute
Added AttributeAware::removeAttribute()
2 parents 0336a4d + 4c0a334 commit 9159d94

12 files changed

Lines changed: 86 additions & 3 deletions

src/Attribute/AttributeAware.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public function getAttribute($name, $default = null);
2626
*/
2727
public function setAttribute($name, $value);
2828

29+
/**
30+
* Removes a single attribute with the given $name
31+
*
32+
* @param string $name
33+
*/
34+
public function removeAttribute($name);
35+
2936
/**
3037
* get a container for all attributes
3138
*

src/Attribute/AttributeBag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface AttributeBag extends AttributeAware
99
{
1010
// public function getAttribute($name, $default);
1111
// public function setAttribute($name, $value);
12+
// public function removeAttribute();
1213
// public function getAttributeBag();
1314

1415
/**

src/Attribute/AttributeBagContainer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public function setAttribute($name, $value)
4141
return $this;
4242
}
4343

44+
/**
45+
* Removes a single attribute with the given $name
46+
*
47+
* @param string $name
48+
*/
49+
public function removeAttribute($name)
50+
{
51+
unset($this->attributes[$name]);
52+
}
53+
4454
/**
4555
* get an array of all attributes
4656
*

src/Attribute/AttributeBagNamespaced.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public function setAttribute($name, $value)
6868
$this->bag->setAttribute($this->prefix . $name, $value);
6969
}
7070

71+
/**
72+
* Removes a single attribute with the given $name
73+
*
74+
* @param string $name
75+
*/
76+
public function removeAttribute($name)
77+
{
78+
$this->bag->removeAttribute($this->prefix . $name);
79+
}
80+
7181
/**
7282
* get an array of all attributes
7383
*

src/Attribute/AttributeBagReference.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public function setAttribute($name, $value)
5555
return $this;
5656
}
5757

58+
/**
59+
* Removes a single attribute with the given $name
60+
*
61+
* @param string $name
62+
*/
63+
public function removeAttribute($name)
64+
{
65+
unset($this->attributes[$name]);
66+
}
67+
5868
/**
5969
* get an array of all attributes
6070
*

src/Edge/Base.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ public function setAttribute($name, $value)
303303
$this->attributes[$name] = $value;
304304
}
305305

306+
public function removeAttribute($name)
307+
{
308+
unset($this->attributes[$name]);
309+
}
310+
306311
public function getAttributeBag()
307312
{
308313
return new AttributeBagReference($this->attributes);

src/Graph.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ public function setAttribute($name, $value)
462462
$this->attributes[$name] = $value;
463463
}
464464

465+
public function removeAttribute($name)
466+
{
467+
unset($this->attributes[$name]);
468+
}
469+
465470
public function getAttributeBag()
466471
{
467472
return new AttributeBagReference($this->attributes);

src/Vertex.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ public function setAttribute($name, $value)
382382
$this->attributes[$name] = $value;
383383
}
384384

385+
public function removeAttribute($name)
386+
{
387+
unset($this->attributes[$name]);
388+
}
389+
385390
public function getAttributeBag()
386391
{
387392
return new AttributeBagReference($this->attributes);

tests/Attribute/AbstractAttributeAwareTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public function testAttributeSetGetDefault(AttributeAware $entity)
2727
$this->assertEquals('default', $entity->getAttribute('unknown', 'default'));
2828
}
2929

30+
/**
31+
* @depends testAttributeAwareInterface
32+
* @param AttributeAware $entity
33+
*/
34+
public function testAttributeSetRemoveGet(AttributeAware $entity)
35+
{
36+
$entity->setAttribute('test', 'value');
37+
$this->assertEquals('value', $entity->getAttribute('test'));
38+
39+
$entity->removeAttribute('test');
40+
$this->assertEquals(null, $entity->getAttribute('test'));
41+
}
42+
3043
/**
3144
* @depends testAttributeAwareInterface
3245
* @param AttributeAware $entity

tests/Attribute/AttributeBagContainerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
use Fhaculty\Graph\Attribute\AttributeBagContainer;
44

5-
class AttributeBagContainerTest extends TestCase
5+
class AttributeBagContainerTest extends AbstractAttributeAwareTest
66
{
7+
protected function createAttributeAware()
8+
{
9+
return new AttributeBagContainer();
10+
}
11+
712
public function testEmpty()
813
{
914
$bag = new AttributeBagContainer();

0 commit comments

Comments
 (0)