4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Attribute;
15 * This class relates to session attribute storage.
17 class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
19 private $name = 'attributes';
29 protected $attributes = array();
34 * @param string $storageKey The key used to store attributes in the session
36 public function __construct($storageKey = '_sf2_attributes')
38 $this->storageKey = $storageKey;
44 public function getName()
49 public function setName($name)
57 public function initialize(array &$attributes)
59 $this->attributes = &$attributes;
65 public function getStorageKey()
67 return $this->storageKey;
73 public function has($name)
75 return array_key_exists($name, $this->attributes);
81 public function get($name, $default = null)
83 return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
89 public function set($name, $value)
91 $this->attributes[$name] = $value;
99 return $this->attributes;
105 public function replace(array $attributes)
107 $this->attributes = array();
108 foreach ($attributes as $key => $value) {
109 $this->set($key, $value);
116 public function remove($name)
119 if (array_key_exists($name, $this->attributes)) {
120 $retval = $this->attributes[$name];
121 unset($this->attributes[$name]);
130 public function clear()
132 $return = $this->attributes;
133 $this->attributes = array();
139 * Returns an iterator for attributes.
141 * @return \ArrayIterator An \ArrayIterator instance
143 public function getIterator()
145 return new \ArrayIterator($this->attributes);
149 * Returns the number of attributes.
151 * @return int The number of attributes
153 public function count()
155 return count($this->attributes);