1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Session\Attribute;
13:
14: 15: 16: 17: 18: 19:
20: class NamespacedAttributeBag extends AttributeBag
21: {
22: 23: 24: 25: 26:
27: private $namespaceCharacter;
28:
29: 30: 31: 32: 33: 34:
35: public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
36: {
37: $this->namespaceCharacter = $namespaceCharacter;
38: parent::__construct($storageKey);
39: }
40:
41: 42: 43:
44: public function has($name)
45: {
46: $attributes = $this->resolveAttributePath($name);
47: $name = $this->resolveKey($name);
48:
49: if (null === $attributes) {
50: return false;
51: }
52:
53: return array_key_exists($name, $attributes);
54: }
55:
56: 57: 58:
59: public function get($name, $default = null)
60: {
61: $attributes = $this->resolveAttributePath($name);
62: $name = $this->resolveKey($name);
63:
64: if (null === $attributes) {
65: return $default;
66: }
67:
68: return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
69: }
70:
71: 72: 73:
74: public function set($name, $value)
75: {
76: $attributes = & $this->resolveAttributePath($name, true);
77: $name = $this->resolveKey($name);
78: $attributes[$name] = $value;
79: }
80:
81: 82: 83:
84: public function remove($name)
85: {
86: $retval = null;
87: $attributes = & $this->resolveAttributePath($name);
88: $name = $this->resolveKey($name);
89: if (null !== $attributes && array_key_exists($name, $attributes)) {
90: $retval = $attributes[$name];
91: unset($attributes[$name]);
92: }
93:
94: return $retval;
95: }
96:
97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
107: protected function &resolveAttributePath($name, $writeContext = false)
108: {
109: $array = & $this->attributes;
110: $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
111:
112:
113: if (!$name) {
114: return $array;
115: }
116:
117: $parts = explode($this->namespaceCharacter, $name);
118: if (count($parts) < 2) {
119: if (!$writeContext) {
120: return $array;
121: }
122:
123: $array[$parts[0]] = array();
124:
125: return $array;
126: }
127:
128: unset($parts[count($parts)-1]);
129:
130: foreach ($parts as $part) {
131: if (null !== $array && !array_key_exists($part, $array)) {
132: $array[$part] = $writeContext ? array() : null;
133: }
134:
135: $array = & $array[$part];
136: }
137:
138: return $array;
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148: 149:
150: protected function resolveKey($name)
151: {
152: if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
153: $name = substr($name, $pos+1);
154: }
155:
156: return $name;
157: }
158: }
159: