33namespace 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 */
815class 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 ;
0 commit comments