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: return array_key_exists($name, $attributes);
50: }
51:
52: 53: 54:
55: public function get($name, $default = null)
56: {
57: $attributes = $this->resolveAttributePath($name);
58: $name = $this->resolveKey($name);
59:
60: return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
61: }
62:
63: 64: 65:
66: public function set($name, $value)
67: {
68: $attributes = & $this->resolveAttributePath($name, true);
69: $name = $this->resolveKey($name);
70: $attributes[$name] = $value;
71: }
72:
73: 74: 75:
76: public function remove($name)
77: {
78: $retval = null;
79: $attributes = & $this->resolveAttributePath($name);
80: $name = $this->resolveKey($name);
81: if (array_key_exists($name, $attributes)) {
82: $retval = $attributes[$name];
83: unset($attributes[$name]);
84: }
85:
86: return $retval;
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97: 98:
99: protected function &resolveAttributePath($name, $writeContext = false)
100: {
101: $array = & $this->attributes;
102: $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
103:
104:
105: if (!$name) {
106: return $array;
107: }
108:
109: $parts = explode($this->namespaceCharacter, $name);
110: if (count($parts) < 2) {
111: if (!$writeContext) {
112: return $array;
113: }
114:
115: $array[$parts[0]] = array();
116:
117: return $array;
118: }
119:
120: unset($parts[count($parts)-1]);
121:
122: foreach ($parts as $part) {
123: if (!array_key_exists($part, $array)) {
124: if (!$writeContext) {
125: return $array;
126: }
127:
128: $array[$part] = array();
129: }
130:
131: $array = & $array[$part];
132: }
133:
134: return $array;
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145:
146: protected function resolveKey($name)
147: {
148: if (strpos($name, $this->namespaceCharacter) !== false) {
149: $name = substr($name, strrpos($name, $this->namespaceCharacter)+1, strlen($name));
150: }
151:
152: return $name;
153: }
154: }
155: