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\Flash;
13:
14: /**
15: * FlashBag flash message container.
16: *
17: * @author Drak <drak@zikula.org>
18: */
19: class FlashBag implements FlashBagInterface, \IteratorAggregate, \Countable
20: {
21: private $name = 'flashes';
22:
23: /**
24: * Flash messages.
25: *
26: * @var array
27: */
28: private $flashes = array();
29:
30: /**
31: * The storage key for flashes in the session
32: *
33: * @var string
34: */
35: private $storageKey;
36:
37: /**
38: * Constructor.
39: *
40: * @param string $storageKey The key used to store flashes in the session.
41: */
42: public function __construct($storageKey = '_sf2_flashes')
43: {
44: $this->storageKey = $storageKey;
45: }
46:
47: /**
48: * {@inheritdoc}
49: */
50: public function getName()
51: {
52: return $this->name;
53: }
54:
55: public function setName($name)
56: {
57: $this->name = $name;
58: }
59:
60: /**
61: * {@inheritdoc}
62: */
63: public function initialize(array &$flashes)
64: {
65: $this->flashes = &$flashes;
66: }
67:
68: /**
69: * {@inheritdoc}
70: */
71: public function add($type, $message)
72: {
73: $this->flashes[$type][] = $message;
74: }
75:
76: /**
77: * {@inheritdoc}
78: */
79: public function peek($type, array $default =array())
80: {
81: return $this->has($type) ? $this->flashes[$type] : $default;
82: }
83:
84: /**
85: * {@inheritdoc}
86: */
87: public function peekAll()
88: {
89: return $this->flashes;
90: }
91:
92: /**
93: * {@inheritdoc}
94: */
95: public function get($type, array $default = array())
96: {
97: if (!$this->has($type)) {
98: return $default;
99: }
100:
101: $return = $this->flashes[$type];
102:
103: unset($this->flashes[$type]);
104:
105: return $return;
106: }
107:
108: /**
109: * {@inheritdoc}
110: */
111: public function all()
112: {
113: $return = $this->peekAll();
114: $this->flashes = array();
115:
116: return $return;
117: }
118:
119: /**
120: * {@inheritdoc}
121: */
122: public function set($type, $messages)
123: {
124: $this->flashes[$type] = (array) $messages;
125: }
126:
127: /**
128: * {@inheritdoc}
129: */
130: public function setAll(array $messages)
131: {
132: $this->flashes = $messages;
133: }
134:
135: /**
136: * {@inheritdoc}
137: */
138: public function has($type)
139: {
140: return array_key_exists($type, $this->flashes) && $this->flashes[$type];
141: }
142:
143: /**
144: * {@inheritdoc}
145: */
146: public function keys()
147: {
148: return array_keys($this->flashes);
149: }
150:
151: /**
152: * {@inheritdoc}
153: */
154: public function getStorageKey()
155: {
156: return $this->storageKey;
157: }
158:
159: /**
160: * {@inheritdoc}
161: */
162: public function clear()
163: {
164: return $this->all();
165: }
166:
167: /**
168: * Returns an iterator for flashes.
169: *
170: * @return \ArrayIterator An \ArrayIterator instance
171: */
172: public function getIterator()
173: {
174: return new \ArrayIterator($this->all());
175: }
176:
177: /**
178: * Returns the number of flashes.
179: *
180: * This method does not work.
181: *
182: * @deprecated in 2.2, removed in 2.3
183: * @see https://github.com/symfony/symfony/issues/6408
184: *
185: * @return int The number of flashes
186: */
187: public function count()
188: {
189: trigger_error(sprintf('%s() is deprecated since 2.2 and will be removed in 2.3', __METHOD__), E_USER_DEPRECATED);
190:
191: return count($this->flashes);
192: }
193: }
194: