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';
22 protected $attributes = array();
25 * @param string $storageKey The key used to store attributes in the session
27 public function __construct($storageKey = '_sf2_attributes')
29 $this->storageKey
= $storageKey;
35 public function getName()
40 public function setName($name)
48 public function initialize(array &$attributes)
50 $this->attributes
= &$attributes;
56 public function getStorageKey()
58 return $this->storageKey
;
64 public function has($name)
66 return array_key_exists($name, $this->attributes
);
72 public function get($name, $default = null)
74 return array_key_exists($name, $this->attributes
) ?
$this->attributes
[$name] : $default;
80 public function set($name, $value)
82 $this->attributes
[$name] = $value;
90 return $this->attributes
;
96 public function replace(array $attributes)
98 $this->attributes
= array();
99 foreach ($attributes as $key => $value) {
100 $this->set($key, $value);
107 public function remove($name)
110 if (array_key_exists($name, $this->attributes
)) {
111 $retval = $this->attributes
[$name];
112 unset($this->attributes
[$name]);
121 public function clear()
123 $return = $this->attributes
;
124 $this->attributes
= array();
130 * Returns an iterator for attributes.
132 * @return \ArrayIterator An \ArrayIterator instance
134 public function getIterator()
136 return new \
ArrayIterator($this->attributes
);
140 * Returns the number of attributes.
142 * @return int The number of attributes
144 public function count()
146 return count($this->attributes
);