Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-foundation / Session / Attribute / NamespacedAttributeBag.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Session\Attribute;
13
14 /**
15  * This class provides structured storage of session attributes using
16  * a name spacing character in the key.
17  *
18  * @author Drak <drak@zikula.org>
19  */
20 class NamespacedAttributeBag extends AttributeBag
21 {
22     /**
23      * Namespace character.
24      *
25      * @var string
26      */
27     private $namespaceCharacter;
28
29     /**
30      * Constructor.
31      *
32      * @param string $storageKey         Session storage key
33      * @param string $namespaceCharacter Namespace character to use in keys
34      */
35     public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
36     {
37         $this->namespaceCharacter = $namespaceCharacter;
38         parent::__construct($storageKey);
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     public function has($name)
45     {
46         // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
47         $attributes = $this->resolveAttributePath($name);
48         $name = $this->resolveKey($name);
49
50         if (null === $attributes) {
51             return false;
52         }
53
54         return array_key_exists($name, $attributes);
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function get($name, $default = null)
61     {
62         // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
63         $attributes = $this->resolveAttributePath($name);
64         $name = $this->resolveKey($name);
65
66         if (null === $attributes) {
67             return $default;
68         }
69
70         return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     public function set($name, $value)
77     {
78         $attributes = &$this->resolveAttributePath($name, true);
79         $name = $this->resolveKey($name);
80         $attributes[$name] = $value;
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     public function remove($name)
87     {
88         $retval = null;
89         $attributes = &$this->resolveAttributePath($name);
90         $name = $this->resolveKey($name);
91         if (null !== $attributes && array_key_exists($name, $attributes)) {
92             $retval = $attributes[$name];
93             unset($attributes[$name]);
94         }
95
96         return $retval;
97     }
98
99     /**
100      * Resolves a path in attributes property and returns it as a reference.
101      *
102      * This method allows structured namespacing of session attributes.
103      *
104      * @param string $name         Key name
105      * @param bool   $writeContext Write context, default false
106      *
107      * @return array
108      */
109     protected function &resolveAttributePath($name, $writeContext = false)
110     {
111         $array = &$this->attributes;
112         $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
113
114         // Check if there is anything to do, else return
115         if (!$name) {
116             return $array;
117         }
118
119         $parts = explode($this->namespaceCharacter, $name);
120         if (count($parts) < 2) {
121             if (!$writeContext) {
122                 return $array;
123             }
124
125             $array[$parts[0]] = array();
126
127             return $array;
128         }
129
130         unset($parts[count($parts) - 1]);
131
132         foreach ($parts as $part) {
133             if (null !== $array && !array_key_exists($part, $array)) {
134                 $array[$part] = $writeContext ? array() : null;
135             }
136
137             $array = &$array[$part];
138         }
139
140         return $array;
141     }
142
143     /**
144      * Resolves the key from the name.
145      *
146      * This is the last part in a dot separated string.
147      *
148      * @param string $name
149      *
150      * @return string
151      */
152     protected function resolveKey($name)
153     {
154         if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
155             $name = substr($name, $pos + 1);
156         }
157
158         return $name;
159     }
160 }