Skip to content

Commit 87a3dd3

Browse files
committed
Merge pull request #111 from nubs/attributes
Update documentation of attributes
2 parents 0678f50 + 45d1ce9 commit 87a3dd3

4 files changed

Lines changed: 148 additions & 2 deletions

File tree

lib/Fhaculty/Graph/Attribute/AttributeBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
interface AttributeBag extends AttributeAware
99
{
10-
// public function getAttribute($name);
10+
// public function getAttribute($name, $default);
1111
// public function setAttribute($name, $value);
1212
// public function getAttributeBag();
1313

lib/Fhaculty/Graph/Attribute/AttributeBagContainer.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,73 @@
22

33
namespace Fhaculty\Graph\Attribute;
44

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+
*/
511
class AttributeBagContainer implements AttributeBag
612
{
13+
/**
14+
* @var array
15+
*/
716
private $attributes = array();
817

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+
*/
925
public function getAttribute($name, $default = null)
1026
{
1127
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
1228
}
1329

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+
*/
1437
public function setAttribute($name, $value)
1538
{
1639
$this->attributes[$name] = $value;
1740

1841
return $this;
1942
}
2043

44+
/**
45+
* get an array of all attributes
46+
*
47+
* @return array
48+
*/
2149
public function getAttributes()
2250
{
2351
return $this->attributes;
2452
}
2553

54+
/**
55+
* set an array of additional attributes
56+
*
57+
* @param array $attributes
58+
* @return self For a fluid interface.
59+
*/
2660
public function setAttributes(array $attributes)
2761
{
2862
$this->attributes = $attributes + $this->attributes;
2963

3064
return $this;
3165
}
3266

67+
/**
68+
* get a container for all attributes
69+
*
70+
* @return AttributeBag
71+
*/
3372
public function getAttributeBag()
3473
{
3574
return $this;

lib/Fhaculty/Graph/Attribute/AttributeBagNamespaced.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,34 @@
33
namespace Fhaculty\Graph\Attribute;
44

55
/**
6-
* An attribute bag that automatically prefixes a given namespace
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".
714
*/
815
class AttributeBagNamespaced implements AttributeBag
916
{
17+
/**
18+
* @var AttributeBag
19+
*/
1020
private $bag;
21+
22+
/**
23+
* @var string
24+
*/
1125
private $prefix;
1226

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+
*/
1334
public function __construct(AttributeAware $bag, $prefix)
1435
{
1536
if (!($bag instanceof AttributeBag)) {
@@ -19,16 +40,41 @@ public function __construct(AttributeAware $bag, $prefix)
1940
$this->prefix = $prefix;
2041
}
2142

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+
*/
2252
public function getAttribute($name, $default = null)
2353
{
2454
return $this->bag->getAttribute($this->prefix . $name, $default);
2555
}
2656

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+
*/
2766
public function setAttribute($name, $value)
2867
{
2968
$this->bag->setAttribute($this->prefix . $name, $value);
3069
}
3170

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+
*/
3278
public function getAttributes()
3379
{
3480
$attributes = array();
@@ -43,13 +89,26 @@ public function getAttributes()
4389
return $attributes;
4490
}
4591

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+
*/
46100
public function setAttributes(array $attributes)
47101
{
48102
foreach ($attributes as $name => $value) {
49103
$this->bag->setAttribute($this->prefix . $name, $value);
50104
}
51105
}
52106

107+
/**
108+
* get a container for all attributes
109+
*
110+
* @return AttributeBag
111+
*/
53112
public function getAttributeBag()
54113
{
55114
return $this;

lib/Fhaculty/Graph/Attribute/AttributeBagReference.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,87 @@
22

33
namespace Fhaculty\Graph\Attribute;
44

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+
*/
512
class AttributeBagReference implements AttributeBag
613
{
14+
/**
15+
* @var array
16+
*/
717
private $attributes;
818

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+
*/
927
public function __construct(array &$attributes)
1028
{
1129
$this->attributes =& $attributes;
1230
}
1331

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+
*/
1439
public function getAttribute($name, $default = null)
1540
{
1641
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
1742
}
1843

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+
*/
1951
public function setAttribute($name, $value)
2052
{
2153
$this->attributes[$name] = $value;
2254

2355
return $this;
2456
}
2557

58+
/**
59+
* get an array of all attributes
60+
*
61+
* @return array
62+
*/
2663
public function getAttributes()
2764
{
2865
return $this->attributes;
2966
}
3067

68+
/**
69+
* set an array of additional attributes
70+
*
71+
* @param array $attributes
72+
* @return self For a fluid interface.
73+
*/
3174
public function setAttributes(array $attributes)
3275
{
3376
$this->attributes = $attributes + $this->attributes;
3477

3578
return $this;
3679
}
3780

81+
/**
82+
* get a container for all attributes
83+
*
84+
* @return AttributeBag
85+
*/
3886
public function getAttributeBag()
3987
{
4088
return $this;

0 commit comments

Comments
 (0)