Skip to content

Commit 0678f50

Browse files
committed
AttributeAware::getAttribute() now accepts a default value
1 parent 7737c07 commit 0678f50

10 files changed

Lines changed: 23 additions & 15 deletions

File tree

lib/Fhaculty/Graph/Attribute/AttributeAware.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
interface AttributeAware
1111
{
1212
/**
13-
* get a single attribute with the given $name
13+
* get a single attribute with the given $name (or return $default if attribute was not found)
1414
*
1515
* @param string $name
16-
* @return mixed|null
16+
* @param mixed $default to return if attribute was not found
17+
* @return mixed
1718
*/
18-
public function getAttribute($name);
19+
public function getAttribute($name, $default = null);
1920

2021
/**
2122
* set a single attribute with the given $name to given $value

lib/Fhaculty/Graph/Attribute/AttributeBagContainer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class AttributeBagContainer implements AttributeBag
66
{
77
private $attributes = array();
88

9-
public function getAttribute($name)
9+
public function getAttribute($name, $default = null)
1010
{
11-
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
11+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
1212
}
1313

1414
public function setAttribute($name, $value)

lib/Fhaculty/Graph/Attribute/AttributeBagNamespaced.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public function __construct(AttributeAware $bag, $prefix)
1919
$this->prefix = $prefix;
2020
}
2121

22-
public function getAttribute($name)
22+
public function getAttribute($name, $default = null)
2323
{
24-
return $this->bag->getAttribute($this->prefix . $name);
24+
return $this->bag->getAttribute($this->prefix . $name, $default);
2525
}
2626

2727
public function setAttribute($name, $value)

lib/Fhaculty/Graph/Attribute/AttributeBagReference.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public function __construct(array &$attributes)
1111
$this->attributes =& $attributes;
1212
}
1313

14-
public function getAttribute($name)
14+
public function getAttribute($name, $default = null)
1515
{
16-
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
16+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
1717
}
1818

1919
public function setAttribute($name, $value)

lib/Fhaculty/Graph/Edge/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ private function __clone()
293293
// @codeCoverageIgnoreEnd
294294
}
295295

296-
public function getAttribute($name)
296+
public function getAttribute($name, $default = null)
297297
{
298-
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
298+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
299299
}
300300

301301
public function setAttribute($name, $value)

lib/Fhaculty/Graph/Graph.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ public function __toString()
501501
return $this->getExporter()->getOutput($this);
502502
}
503503

504-
public function getAttribute($name)
504+
public function getAttribute($name, $default = null)
505505
{
506-
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
506+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
507507
}
508508

509509
public function setAttribute($name, $value)

lib/Fhaculty/Graph/Vertex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ private function __clone()
372372
// @codeCoverageIgnoreEnd
373373
}
374374

375-
public function getAttribute($name)
375+
public function getAttribute($name, $default = null)
376376
{
377-
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
377+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
378378
}
379379

380380
public function setAttribute($name, $value)

tests/Fhaculty/Graph/Attribute/AttributeBagContainerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public function testEmpty()
99
$bag = new AttributeBagContainer();
1010

1111
$this->assertNull($bag->getAttribute('unknown'));
12+
$this->assertEquals('default', $bag->getAttribute('unknown', 'default'));
13+
1214
$this->assertEquals(array(), $bag->getAttributes());
1315

1416
$this->assertSame($bag, $bag->getAttributeBag());

tests/Fhaculty/Graph/Attribute/AttributeBagNamespacedTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function testBagContainer()
1818

1919
$this->assertEquals('e', $bag->getAttribute('d'));
2020

21+
$this->assertNull($bag->getAttribute('unknown'));
22+
$this->assertEquals('default', $bag->getAttribute('unknown', 'default'));
23+
2124
$bag->setAttribute('d', 'test');
2225

2326
$this->assertEquals('test', $bag->getAttribute('d'));

tests/Fhaculty/Graph/Attribute/AttributeBagReferenceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public function testEmpty()
1111
$bag = new AttributeBagReference($attributes);
1212

1313
$this->assertNull($bag->getAttribute('unknown'));
14+
$this->assertEquals('default', $bag->getAttribute('unknown', 'default'));
15+
1416
$this->assertEquals(array(), $bag->getAttributes());
1517

1618
$this->assertSame($bag, $bag->getAttributeBag());

0 commit comments

Comments
 (0)