abbf37ee7c33cf4be0a671ecf17c134a5bbdac55
[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     private $namespaceCharacter;
23
24     /**
25      * @param string $storageKey         Session storage key
26      * @param string $namespaceCharacter Namespace character to use in keys
27      */
28     public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
29     {
30         $this->namespaceCharacter = $namespaceCharacter;
31         parent::__construct($storageKey);
32     }
33
34     /**
35      * {@inheritdoc}
36      */
37     public function has($name)
38     {
39         // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
40         $attributes = $this->resolveAttributePath($name);
41         $name = $this->resolveKey($name);
42
43         if (null === $attributes) {
44             return false;
45         }
46
47         return array_key_exists($name, $attributes);
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     public function get($name, $default = null)
54     {
55         // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
56         $attributes = $this->resolveAttributePath($name);
57         $name = $this->resolveKey($name);
58
59         if (null === $attributes) {
60             return $default;
61         }
62
63         return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     public function set($name, $value)
70     {
71         $attributes = &$this->resolveAttributePath($name, true);
72         $name = $this->resolveKey($name);
73         $attributes[$name] = $value;
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function remove($name)
80     {
81         $retval = null;
82         $attributes = &$this->resolveAttributePath($name);
83         $name = $this->resolveKey($name);
84         if (null !== $attributes && array_key_exists($name, $attributes)) {
85             $retval = $attributes[$name];
86             unset($attributes[$name]);
87         }
88
89         return $retval;
90     }
91
92     /**
93      * Resolves a path in attributes property and returns it as a reference.
94      *
95      * This method allows structured namespacing of session attributes.
96      *
97      * @param string $name         Key name
98      * @param bool   $writeContext Write context, default false
99      *
100      * @return array
101      */
102     protected function &resolveAttributePath($name, $writeContext = false)
103     {
104         $array = &$this->attributes;
105         $name = (0 === strpos($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
106
107         // Check if there is anything to do, else return
108         if (!$name) {
109             return $array;
110         }
111
112         $parts = explode($this->namespaceCharacter, $name);
113         if (count($parts) < 2) {
114             if (!$writeContext) {
115                 return $array;
116             }
117
118             $array[$parts[0]] = array();
119
120             return $array;
121         }
122
123         unset($parts[count($parts) - 1]);
124
125         foreach ($parts as $part) {
126             if (null !== $array && !array_key_exists($part, $array)) {
127                 $array[$part] = $writeContext ? array() : null;
128             }
129
130             $array = &$array[$part];
131         }
132
133         return $array;
134     }
135
136     /**
137      * Resolves the key from the name.
138      *
139      * This is the last part in a dot separated string.
140      *
141      * @param string $name
142      *
143      * @return string
144      */
145     protected function resolveKey($name)
146     {
147         if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
148             $name = substr($name, $pos + 1);
149         }
150
151         return $name;
152     }
153 }