Skip to content

Commit 015849b

Browse files
committed
Merge pull request #103 from clue/attributes
Add general purpose Attributes, replace Layoutable
2 parents 5b60273 + 87a3dd3 commit 015849b

17 files changed

Lines changed: 593 additions & 143 deletions

lib/Fhaculty/Graph/Algorithm/MaximumMatching/Flow.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function getEdges()
3030
// create temporary flow graph with supersource and supersink
3131
$graphFlow = $this->graph->createGraphCloneEdgeless();
3232

33-
$superSource = $graphFlow->createVertex()->setLayoutAttribute('label', 's*');
34-
$superSink = $graphFlow->createVertex()->setLayoutAttribute('label', 't*');
33+
$superSource = $graphFlow->createVertex();
34+
$superSink = $graphFlow->createVertex();
3535

3636
$groups = $alg->getGroups();
3737
$groupA = $groups[0];

lib/Fhaculty/Graph/Algorithm/MinimumCostFlow/CycleCanceling.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function createGraph()
1919
// create resulting graph with supersource and supersink
2020
$resultGraph = $this->graph->createGraphClone();
2121

22-
$superSource = $resultGraph->createVertex()->setLayoutAttribute('label', 's*');
23-
$superSink = $resultGraph->createVertex()->setLayoutAttribute('label', 't*');
22+
$superSource = $resultGraph->createVertex();
23+
$superSink = $resultGraph->createVertex();
2424

2525
$sumBalance = 0;
2626

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Attribute;
4+
5+
/**
6+
* Implemented by any entity that is aware of additional attributes
7+
*
8+
* Each attribute consists of a name (string) and an arbitrary value.
9+
*/
10+
interface AttributeAware
11+
{
12+
/**
13+
* get a single attribute with the given $name (or return $default if attribute was not found)
14+
*
15+
* @param string $name
16+
* @param mixed $default to return if attribute was not found
17+
* @return mixed
18+
*/
19+
public function getAttribute($name, $default = null);
20+
21+
/**
22+
* set a single attribute with the given $name to given $value
23+
*
24+
* @param string $name
25+
* @param mixed $value
26+
*/
27+
public function setAttribute($name, $value);
28+
29+
/**
30+
* get a container for all attributes
31+
*
32+
* @return AttributeBag
33+
*/
34+
public function getAttributeBag();
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Attribute;
4+
5+
/**
6+
* Interface to container that represents multiple attributes
7+
*/
8+
interface AttributeBag extends AttributeAware
9+
{
10+
// public function getAttribute($name, $default);
11+
// public function setAttribute($name, $value);
12+
// public function getAttributeBag();
13+
14+
/**
15+
* set an array of additional attributes
16+
*
17+
* @param array $attributes
18+
*/
19+
public function setAttributes(array $attributes);
20+
21+
/**
22+
* get an array of all attributes
23+
*
24+
* @return array
25+
*/
26+
public function getAttributes();
27+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Attribute;
4+
5+
/**
6+
* A fairly standard AttributeBag container.
7+
*
8+
* This container passes and returns attributes by value. It is mutable,
9+
* however, so multiple references to the container will update in kind.
10+
*/
11+
class AttributeBagContainer implements AttributeBag
12+
{
13+
/**
14+
* @var array
15+
*/
16+
private $attributes = array();
17+
18+
/**
19+
* get a single attribute with the given $name (or return $default if attribute was not found)
20+
*
21+
* @param string $name
22+
* @param mixed $default to return if attribute was not found
23+
* @return mixed
24+
*/
25+
public function getAttribute($name, $default = null)
26+
{
27+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
28+
}
29+
30+
/**
31+
* set a single attribute with the given $name to given $value
32+
*
33+
* @param string $name
34+
* @param mixed $value
35+
* @return self For a fluid interface.
36+
*/
37+
public function setAttribute($name, $value)
38+
{
39+
$this->attributes[$name] = $value;
40+
41+
return $this;
42+
}
43+
44+
/**
45+
* get an array of all attributes
46+
*
47+
* @return array
48+
*/
49+
public function getAttributes()
50+
{
51+
return $this->attributes;
52+
}
53+
54+
/**
55+
* set an array of additional attributes
56+
*
57+
* @param array $attributes
58+
* @return self For a fluid interface.
59+
*/
60+
public function setAttributes(array $attributes)
61+
{
62+
$this->attributes = $attributes + $this->attributes;
63+
64+
return $this;
65+
}
66+
67+
/**
68+
* get a container for all attributes
69+
*
70+
* @return AttributeBag
71+
*/
72+
public function getAttributeBag()
73+
{
74+
return $this;
75+
}
76+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Attribute;
4+
5+
/**
6+
* An attribute bag that automatically prefixes a given namespace.
7+
*
8+
* For example, you can use this class to prefix the attributes using a vendor
9+
* name, like "myvendor.item.". If another vendor shares the base attribute
10+
* bag, it can use a different prefix, like "otherProduct.item.". This allows
11+
* both libraries to have attributes with the same name without having them
12+
* conflict. For example, the attribute "id" would be stored separately as
13+
* "myvendor.item.id" and "otherProduct.item.id".
14+
*/
15+
class AttributeBagNamespaced implements AttributeBag
16+
{
17+
/**
18+
* @var AttributeBag
19+
*/
20+
private $bag;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $prefix;
26+
27+
/**
28+
* Initialize the attribute bag with a prefix to use as a namespace for the attributes.
29+
*
30+
* @param AttributeAware $bag The bag to store the prefixed attributes in.
31+
* @param string $prefix The prefix to prepend to all attributes before
32+
* storage. This prefix acts as a namespace to separate attributes.
33+
*/
34+
public function __construct(AttributeAware $bag, $prefix)
35+
{
36+
if (!($bag instanceof AttributeBag)) {
37+
$bag = $bag->getAttributeBag();
38+
}
39+
$this->bag = $bag;
40+
$this->prefix = $prefix;
41+
}
42+
43+
/**
44+
* get a single attribute with the given $name (or return $default if attribute was not found)
45+
*
46+
* This prefixes the attribute name before requesting from the base bag.
47+
*
48+
* @param string $name
49+
* @param mixed $default to return if attribute was not found
50+
* @return mixed
51+
*/
52+
public function getAttribute($name, $default = null)
53+
{
54+
return $this->bag->getAttribute($this->prefix . $name, $default);
55+
}
56+
57+
/**
58+
* set a single attribute with the given $name to given $value
59+
*
60+
* This prefixes the attribute name before setting in the base bag.
61+
*
62+
* @param string $name
63+
* @param mixed $value
64+
* @return void
65+
*/
66+
public function setAttribute($name, $value)
67+
{
68+
$this->bag->setAttribute($this->prefix . $name, $value);
69+
}
70+
71+
/**
72+
* get an array of all attributes
73+
*
74+
* The prefix will not be included in the returned attribute keys.
75+
*
76+
* @return array
77+
*/
78+
public function getAttributes()
79+
{
80+
$attributes = array();
81+
$len = strlen($this->prefix);
82+
83+
foreach ($this->bag->getAttributes() as $name => $value) {
84+
if (strpos($name, $this->prefix) === 0) {
85+
$attributes[substr($name, $len)] = $value;
86+
}
87+
}
88+
89+
return $attributes;
90+
}
91+
92+
/**
93+
* set an array of additional attributes
94+
*
95+
* Each attribute is prefixed before setting in the base bag.
96+
*
97+
* @param array $attributes
98+
* @return void
99+
*/
100+
public function setAttributes(array $attributes)
101+
{
102+
foreach ($attributes as $name => $value) {
103+
$this->bag->setAttribute($this->prefix . $name, $value);
104+
}
105+
}
106+
107+
/**
108+
* get a container for all attributes
109+
*
110+
* @return AttributeBag
111+
*/
112+
public function getAttributeBag()
113+
{
114+
return $this;
115+
}
116+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Attribute;
4+
5+
/**
6+
* The basic attribute bag, but using a reference to the base attribute array.
7+
*
8+
* This container passes and returns attributes by value, but stores them in a
9+
* pass-by-reference array. It is mutable, however, so multiple references to
10+
* the container will update in kind.
11+
*/
12+
class AttributeBagReference implements AttributeBag
13+
{
14+
/**
15+
* @var array
16+
*/
17+
private $attributes;
18+
19+
/**
20+
* Initialize the attribute bag with the base attribute array.
21+
*
22+
* The given array is pass-by-reference, so updates to the array here or in
23+
* calling code will be reflected everywhere.
24+
*
25+
* @param array $attributes The pass-by-reference attributes.
26+
*/
27+
public function __construct(array &$attributes)
28+
{
29+
$this->attributes =& $attributes;
30+
}
31+
32+
/**
33+
* get a single attribute with the given $name (or return $default if attribute was not found)
34+
*
35+
* @param string $name
36+
* @param mixed $default to return if attribute was not found
37+
* @return mixed
38+
*/
39+
public function getAttribute($name, $default = null)
40+
{
41+
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
42+
}
43+
44+
/**
45+
* set a single attribute with the given $name to given $value
46+
*
47+
* @param string $name
48+
* @param mixed $value
49+
* @return self For a fluid interface.
50+
*/
51+
public function setAttribute($name, $value)
52+
{
53+
$this->attributes[$name] = $value;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* get an array of all attributes
60+
*
61+
* @return array
62+
*/
63+
public function getAttributes()
64+
{
65+
return $this->attributes;
66+
}
67+
68+
/**
69+
* set an array of additional attributes
70+
*
71+
* @param array $attributes
72+
* @return self For a fluid interface.
73+
*/
74+
public function setAttributes(array $attributes)
75+
{
76+
$this->attributes = $attributes + $this->attributes;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* get a container for all attributes
83+
*
84+
* @return AttributeBag
85+
*/
86+
public function getAttributeBag()
87+
{
88+
return $this;
89+
}
90+
}

0 commit comments

Comments
 (0)