Version 1
[yaffs-website] / vendor / symfony / http-foundation / Session / Attribute / AttributeBag.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 relates to session attribute storage.
16  */
17 class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
18 {
19     private $name = 'attributes';
20
21     /**
22      * @var string
23      */
24     private $storageKey;
25
26     /**
27      * @var array
28      */
29     protected $attributes = array();
30
31     /**
32      * Constructor.
33      *
34      * @param string $storageKey The key used to store attributes in the session
35      */
36     public function __construct($storageKey = '_sf2_attributes')
37     {
38         $this->storageKey = $storageKey;
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     public function getName()
45     {
46         return $this->name;
47     }
48
49     public function setName($name)
50     {
51         $this->name = $name;
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     public function initialize(array &$attributes)
58     {
59         $this->attributes = &$attributes;
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     public function getStorageKey()
66     {
67         return $this->storageKey;
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     public function has($name)
74     {
75         return array_key_exists($name, $this->attributes);
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     public function get($name, $default = null)
82     {
83         return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function set($name, $value)
90     {
91         $this->attributes[$name] = $value;
92     }
93
94     /**
95      * {@inheritdoc}
96      */
97     public function all()
98     {
99         return $this->attributes;
100     }
101
102     /**
103      * {@inheritdoc}
104      */
105     public function replace(array $attributes)
106     {
107         $this->attributes = array();
108         foreach ($attributes as $key => $value) {
109             $this->set($key, $value);
110         }
111     }
112
113     /**
114      * {@inheritdoc}
115      */
116     public function remove($name)
117     {
118         $retval = null;
119         if (array_key_exists($name, $this->attributes)) {
120             $retval = $this->attributes[$name];
121             unset($this->attributes[$name]);
122         }
123
124         return $retval;
125     }
126
127     /**
128      * {@inheritdoc}
129      */
130     public function clear()
131     {
132         $return = $this->attributes;
133         $this->attributes = array();
134
135         return $return;
136     }
137
138     /**
139      * Returns an iterator for attributes.
140      *
141      * @return \ArrayIterator An \ArrayIterator instance
142      */
143     public function getIterator()
144     {
145         return new \ArrayIterator($this->attributes);
146     }
147
148     /**
149      * Returns the number of attributes.
150      *
151      * @return int The number of attributes
152      */
153     public function count()
154     {
155         return count($this->attributes);
156     }
157 }