|
| 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 | +} |
0 commit comments