Skip to content

Commit

Permalink
Merge pull request #174 from clue-labs/chainable-attributes
Browse files Browse the repository at this point in the history
Make attributes chainable
  • Loading branch information
clue authored Oct 2, 2019
2 parents e14d3ac + a726e48 commit e9e3be2
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Attribute/AttributeAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public function getAttribute($name, $default = null);
*
* @param string $name
* @param mixed $value
* @return $this chainable
*/
public function setAttribute($name, $value);

/**
* Removes a single attribute with the given $name
*
* @param string $name
* @return $this chainable
*/
public function removeAttribute($name);

Expand Down
1 change: 1 addition & 0 deletions src/Attribute/AttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface AttributeBag extends AttributeAware
* set an array of additional attributes
*
* @param array $attributes
* @return $this chainable
*/
public function setAttributes(array $attributes);

Expand Down
9 changes: 6 additions & 3 deletions src/Attribute/AttributeBagContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getAttribute($name, $default = null)
*
* @param string $name
* @param mixed $value
* @return self For a fluid interface.
* @return $this chainable
*/
public function setAttribute($name, $value)
{
Expand All @@ -44,11 +44,14 @@ public function setAttribute($name, $value)
/**
* Removes a single attribute with the given $name
*
* @param string $name
* @param string $name
* @return $this chainable
*/
public function removeAttribute($name)
{
unset($this->attributes[$name]);

return $this;
}

/**
Expand All @@ -65,7 +68,7 @@ public function getAttributes()
* set an array of additional attributes
*
* @param array $attributes
* @return self For a fluid interface.
* @return $this chainable
*/
public function setAttributes(array $attributes)
{
Expand Down
13 changes: 10 additions & 3 deletions src/Attribute/AttributeBagNamespaced.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,26 @@ public function getAttribute($name, $default = null)
*
* @param string $name
* @param mixed $value
* @return void
* @return $this chainable
*/
public function setAttribute($name, $value)
{
$this->bag->setAttribute($this->prefix . $name, $value);

return $this;
}

/**
* Removes a single attribute with the given $name
*
* @param string $name
* @param string $name
* @return $this chainable
*/
public function removeAttribute($name)
{
$this->bag->removeAttribute($this->prefix . $name);

return $this;
}

/**
Expand Down Expand Up @@ -105,13 +110,15 @@ public function getAttributes()
* Each attribute is prefixed before setting in the base bag.
*
* @param array $attributes
* @return void
* @return $this chainable
*/
public function setAttributes(array $attributes)
{
foreach ($attributes as $name => $value) {
$this->bag->setAttribute($this->prefix . $name, $value);
}

return $this;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Attribute/AttributeBagReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getAttribute($name, $default = null)
*
* @param string $name
* @param mixed $value
* @return self For a fluid interface.
* @return $this chainable
*/
public function setAttribute($name, $value)
{
Expand All @@ -58,11 +58,14 @@ public function setAttribute($name, $value)
/**
* Removes a single attribute with the given $name
*
* @param string $name
* @param string $name
* @return $this chainable
*/
public function removeAttribute($name)
{
unset($this->attributes[$name]);

return $this;
}

/**
Expand All @@ -79,7 +82,7 @@ public function getAttributes()
* set an array of additional attributes
*
* @param array $attributes
* @return self For a fluid interface.
* @return $this chainable
*/
public function setAttributes(array $attributes)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Edge/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,15 @@ public function getAttribute($name, $default = null)
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;

return $this;
}

public function removeAttribute($name)
{
unset($this->attributes[$name]);

return $this;
}

public function getAttributeBag()
Expand Down
4 changes: 4 additions & 0 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,15 @@ public function getAttribute($name, $default = null)
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;

return $this;
}

public function removeAttribute($name)
{
unset($this->attributes[$name]);

return $this;
}

public function getAttributeBag()
Expand Down
4 changes: 4 additions & 0 deletions src/Vertex.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,15 @@ public function getAttribute($name, $default = null)
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;

return $this;
}

public function removeAttribute($name)
{
unset($this->attributes[$name]);

return $this;
}

public function getAttributeBag()
Expand Down
43 changes: 39 additions & 4 deletions tests/Attribute/AbstractAttributeAwareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,61 @@ public function testAttributeAwareInterface()
* @depends testAttributeAwareInterface
* @param AttributeAware $entity
*/
public function testAttributeSetGetDefault(AttributeAware $entity)
public function testSetAttributeReturnsSelf(AttributeAware $entity)
{
$this->assertSame($entity, $entity->setAttribute('test', 'value'));

return $entity;
}

/**
* @depends testAttributeAwareInterface
* @param AttributeAware $entity
*/
public function testGetAttributeReturnsValueIfItExists(AttributeAware $entity)
{
$entity->setAttribute('test', 'value');

$this->assertEquals('value', $entity->getAttribute('test'));
}

/**
* @depends testAttributeAwareInterface
* @param AttributeAware $entity
*/
public function testGetAttributeReturnsNullIfItDoesNotExist(AttributeAware $entity)
{
$this->assertEquals(null, $entity->getAttribute('unknown'));
}

/**
* @depends testAttributeAwareInterface
* @param AttributeAware $entity
*/
public function testGetAttributeReturnsExpicitDefaultIfItDoesNotExist(AttributeAware $entity)
{
$this->assertEquals('default', $entity->getAttribute('unknown', 'default'));
}

/**
* @depends testAttributeAwareInterface
* @param AttributeAware $entity
*/
public function testAttributeSetRemoveGet(AttributeAware $entity)
public function testRemoveAttributeReturnsSelfIfItDoesNotExist(AttributeAware $entity)
{
$this->assertSame($entity, $entity->removeAttribute('unknown'));
}

/**
* @depends testAttributeAwareInterface
* @param AttributeAware $entity
*/
public function testRemoveAttributeReturnsSelfAndGetAttributeThenReturnsNull(AttributeAware $entity)
{
$entity->setAttribute('test', 'value');
$this->assertEquals('value', $entity->getAttribute('test'));

$entity->removeAttribute('test');
$this->assertSame($entity, $entity->removeAttribute('test'));

$this->assertEquals(null, $entity->getAttribute('test'));
}

Expand Down

0 comments on commit e9e3be2

Please sign in to comment.