1 | <?php |
||
18 | class Segment implements SegmentInterface |
||
19 | { |
||
20 | /** |
||
21 | * |
||
22 | * The session manager. |
||
23 | * |
||
24 | * @var Session |
||
25 | * |
||
26 | */ |
||
27 | protected $session; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * The segment name. |
||
32 | * |
||
33 | * @var string |
||
34 | * |
||
35 | */ |
||
36 | protected $name; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param Session $session The session manager. |
||
43 | * |
||
44 | * @param string $name The segment name. |
||
45 | * |
||
46 | */ |
||
47 | 22 | public function __construct(Session $session, $name) |
|
48 | { |
||
49 | 22 | $this->session = $session; |
|
50 | 22 | $this->name = $name; |
|
51 | 22 | } |
|
52 | |||
53 | /** |
||
54 | * |
||
55 | * Returns the value of a key in the segment. |
||
56 | * |
||
57 | * @param string $key The key in the segment. |
||
58 | * |
||
59 | * @param mixed $alt An alternative value to return if the key is not set. |
||
60 | * |
||
61 | * @return mixed |
||
62 | * |
||
63 | */ |
||
64 | 11 | public function get($key, $alt = null) |
|
71 | |||
72 | /** |
||
73 | * |
||
74 | * Returns the entire segment. |
||
75 | * |
||
76 | * @return mixed |
||
77 | * |
||
78 | */ |
||
79 | 1 | public function getSegment() |
|
86 | |||
87 | /** |
||
88 | * |
||
89 | * Sets the value of a key in the segment. |
||
90 | * |
||
91 | * @param string $key The key to set. |
||
92 | * |
||
93 | * @param mixed $val The value to set it to. |
||
94 | * |
||
95 | */ |
||
96 | 14 | public function set($key, $val) |
|
101 | |||
102 | /** |
||
103 | * |
||
104 | * Clear all data from the segment. |
||
105 | * |
||
106 | * @return null |
||
107 | * |
||
108 | */ |
||
109 | 2 | public function clear() |
|
110 | { |
||
111 | 2 | if ($this->resumeSession()) { |
|
112 | 1 | $_SESSION[$this->name] = array(); |
|
113 | 1 | } |
|
114 | 2 | } |
|
115 | |||
116 | |||
117 | /** |
||
118 | * Remove a key from the segment, or remove the entire segment (including key) from the session |
||
119 | * |
||
120 | * @param null $key |
||
121 | */ |
||
122 | 2 | public function remove($key = null) { |
|
123 | 2 | if ($this->resumeSession()) { |
|
124 | 2 | if($key){ |
|
125 | 1 | if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){ |
|
126 | 1 | unset($_SESSION[$this->name][$key]); |
|
127 | 1 | } |
|
128 | 1 | } else { |
|
129 | 1 | unset($_SESSION[$this->name]); |
|
130 | } |
||
131 | 2 | } |
|
132 | 2 | } |
|
133 | |||
134 | /** |
||
135 | * |
||
136 | * Sets a flash value for the *next* request. |
||
137 | * |
||
138 | * @param string $key The key for the flash value. |
||
139 | * |
||
140 | * @param mixed $val The flash value itself. |
||
141 | * |
||
142 | */ |
||
143 | 3 | public function setFlash($key, $val) |
|
144 | { |
||
145 | 3 | $this->resumeOrStartSession(); |
|
146 | 3 | $_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val; |
|
147 | 3 | } |
|
148 | |||
149 | /** |
||
150 | * |
||
151 | * Gets the flash value for a key in the *current* request. |
||
152 | * |
||
153 | * @param string $key The key for the flash value. |
||
154 | * |
||
155 | * @param mixed $alt An alternative value to return if the key is not set. |
||
156 | * |
||
157 | * @return mixed The flash value itself. |
||
158 | * |
||
159 | */ |
||
160 | 3 | public function getFlash($key, $alt = null) |
|
161 | { |
||
162 | 3 | $this->resumeSession(); |
|
163 | 3 | return isset($_SESSION[Session::FLASH_NOW][$this->name][$key]) |
|
164 | 3 | ? $_SESSION[Session::FLASH_NOW][$this->name][$key] |
|
165 | 3 | : $alt; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * |
||
170 | * Gets all the flash values in the *current* request. |
||
171 | * |
||
172 | * @param mixed $alt An alternative value to return if no flash values are set. |
||
173 | * |
||
174 | * @return mixed The flash values themselves. |
||
175 | * |
||
176 | */ |
||
177 | 1 | public function getAllCurrentFlash($alt = array()) |
|
178 | { |
||
179 | 1 | $this->resumeSession(); |
|
180 | 1 | return isset($_SESSION[Session::FLASH_NOW][$this->name]) |
|
181 | 1 | ? $_SESSION[Session::FLASH_NOW][$this->name] |
|
182 | 1 | : $alt; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * |
||
187 | * Clears flash values for *only* the next request. |
||
188 | * |
||
189 | * @return null |
||
190 | * |
||
191 | */ |
||
192 | 2 | public function clearFlash() |
|
193 | { |
||
194 | 2 | if ($this->resumeSession()) { |
|
195 | 2 | $_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
|
196 | 2 | } |
|
197 | 2 | } |
|
198 | |||
199 | /** |
||
200 | * |
||
201 | * Gets the flash value for a key in the *next* request. |
||
202 | * |
||
203 | * @param string $key The key for the flash value. |
||
204 | * |
||
205 | * @param mixed $alt An alternative value to return if the key is not set. |
||
206 | * |
||
207 | * @return mixed The flash value itself. |
||
208 | * |
||
209 | */ |
||
210 | 2 | public function getFlashNext($key, $alt = null) |
|
217 | |||
218 | /** |
||
219 | * |
||
220 | * Gets all flash values for the *next* request. |
||
221 | * |
||
222 | * @param mixed $alt An alternative value to return if no flash values set. |
||
223 | * |
||
224 | * @return mixed The flash values themselves. |
||
225 | * |
||
226 | */ |
||
227 | 1 | public function getAllNextFlash($alt = array()) |
|
228 | { |
||
229 | 1 | $this->resumeSession(); |
|
230 | 1 | return isset($_SESSION[Session::FLASH_NEXT][$this->name]) |
|
231 | 1 | ? $_SESSION[Session::FLASH_NEXT][$this->name] |
|
232 | 1 | : $alt; |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * |
||
237 | * Sets a flash value for the *next* request *and* the current one. |
||
238 | * |
||
239 | * @param string $key The key for the flash value. |
||
240 | * |
||
241 | * @param mixed $val The flash value itself. |
||
242 | * |
||
243 | */ |
||
244 | 2 | public function setFlashNow($key, $val) |
|
250 | |||
251 | /** |
||
252 | * |
||
253 | * Clears flash values for *both* the next request *and* the current one. |
||
254 | * |
||
255 | * @return null |
||
256 | * |
||
257 | */ |
||
258 | 2 | public function clearFlashNow() |
|
259 | { |
||
260 | 2 | if ($this->resumeSession()) { |
|
261 | 2 | $_SESSION[Session::FLASH_NOW][$this->name] = array(); |
|
262 | 2 | $_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
|
263 | 2 | } |
|
264 | 2 | } |
|
265 | |||
266 | /** |
||
267 | * |
||
268 | * Retains all the current flash values for the next request; values that |
||
269 | * already exist for the next request take precedence. |
||
270 | * |
||
271 | * @return null |
||
272 | * |
||
273 | */ |
||
274 | 2 | public function keepFlash() |
|
275 | { |
||
276 | 2 | if ($this->resumeSession()) { |
|
277 | 2 | $_SESSION[Session::FLASH_NEXT][$this->name] = array_merge( |
|
278 | 2 | $_SESSION[Session::FLASH_NEXT][$this->name], |
|
279 | 2 | $_SESSION[Session::FLASH_NOW][$this->name] |
|
280 | 2 | ); |
|
281 | 2 | } |
|
282 | 2 | } |
|
283 | |||
284 | /** |
||
285 | * |
||
286 | * Loads the segment only if the session has already been started, or if |
||
287 | * a session is available (in which case it resumes the session first). |
||
288 | * |
||
289 | * @return bool |
||
290 | * |
||
291 | */ |
||
292 | 21 | protected function resumeSession() |
|
301 | |||
302 | /** |
||
303 | * |
||
304 | * Sets the segment properties to $_SESSION references. |
||
305 | * |
||
306 | * @return null |
||
307 | * |
||
308 | */ |
||
309 | 18 | protected function load() |
|
323 | |||
324 | /** |
||
325 | * |
||
326 | * Resumes a previous session, or starts a new one, and loads the segment. |
||
327 | * |
||
328 | * @return null |
||
329 | * |
||
330 | */ |
||
331 | 17 | protected function resumeOrStartSession() |
|
338 | } |
||
339 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: