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 | 20 | public function __construct(Session $session, $name) |
|
48 | { |
||
49 | 20 | $this->session = $session; |
|
50 | 20 | $this->name = $name; |
|
51 | 20 | } |
|
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 | 10 | public function get($key, $alt = null) |
|
71 | |||
72 | /** |
||
73 | * |
||
74 | * Returns the entire segment. |
||
75 | * |
||
76 | * @return mixed |
||
77 | * |
||
78 | */ |
||
79 | public function getSegment() |
||
80 | { |
||
81 | 11 | $this->resumeSession(); |
|
82 | return isset($_SESSION[$this->name]) |
||
83 | 11 | ? $_SESSION[$this->name] |
|
84 | 11 | : null; |
|
85 | 11 | } |
|
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 | 2 | * |
|
95 | */ |
||
96 | 2 | public function set($key, $val) |
|
101 | |||
102 | /** |
||
103 | * |
||
104 | * Clear all data from the segment. |
||
105 | * |
||
106 | * @return null |
||
107 | * |
||
108 | */ |
||
109 | public function clear() |
||
110 | 3 | { |
|
111 | if ($this->resumeSession()) { |
||
112 | 3 | $_SESSION[$this->name] = array(); |
|
113 | 3 | } |
|
114 | 3 | } |
|
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 | public function remove($key = null) { |
||
123 | if ($this->resumeSession()) { |
||
124 | if($key){ |
||
125 | if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){ |
||
126 | unset($_SESSION[$this->name][$key]); |
||
127 | 3 | } |
|
128 | } else { |
||
129 | 3 | unset($_SESSION[$this->name]); |
|
130 | 3 | } |
|
131 | 3 | } |
|
132 | 3 | } |
|
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 | 2 | */ |
|
143 | public function setFlash($key, $val) |
||
144 | 2 | { |
|
145 | 2 | $this->resumeOrStartSession(); |
|
146 | 2 | $_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val; |
|
147 | 2 | } |
|
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 | 2 | public function getFlash($key, $alt = null) |
|
161 | { |
||
162 | 2 | $this->resumeSession(); |
|
163 | 2 | return isset($_SESSION[Session::FLASH_NOW][$this->name][$key]) |
|
164 | 2 | ? $_SESSION[Session::FLASH_NOW][$this->name][$key] |
|
165 | 2 | : $alt; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * |
||
170 | * Clears flash values for *only* the next request. |
||
171 | * |
||
172 | * @return null |
||
173 | * |
||
174 | */ |
||
175 | public function clearFlash() |
||
181 | 2 | ||
182 | 2 | /** |
|
183 | * |
||
184 | * Gets the flash value for a key in the *next* request. |
||
185 | * |
||
186 | * @param string $key The key for the flash value. |
||
187 | * |
||
188 | * @param mixed $alt An alternative value to return if the key is not set. |
||
189 | * |
||
190 | * @return mixed The flash value itself. |
||
191 | 2 | * |
|
192 | */ |
||
193 | 2 | public function getFlashNext($key, $alt = null) |
|
200 | |||
201 | /** |
||
202 | * |
||
203 | * Sets a flash value for the *next* request *and* the current one. |
||
204 | * |
||
205 | * @param string $key The key for the flash value. |
||
206 | * |
||
207 | 2 | * @param mixed $val The flash value itself. |
|
208 | * |
||
209 | 2 | */ |
|
210 | 2 | public function setFlashNow($key, $val) |
|
216 | |||
217 | /** |
||
218 | * |
||
219 | * Clears flash values for *both* the next request *and* the current one. |
||
220 | * |
||
221 | * @return null |
||
222 | * |
||
223 | */ |
||
224 | public function clearFlashNow() |
||
231 | |||
232 | 15 | /** |
|
233 | * |
||
234 | * Retains all the current flash values for the next request; values that |
||
235 | * already exist for the next request take precedence. |
||
236 | * |
||
237 | * @return null |
||
238 | * |
||
239 | */ |
||
240 | public function keepFlash() |
||
249 | 15 | ||
250 | 15 | /** |
|
251 | * |
||
252 | 15 | * Loads the segment only if the session has already been started, or if |
|
253 | 15 | * a session is available (in which case it resumes the session first). |
|
254 | 15 | * |
|
255 | 15 | * @return bool |
|
256 | * |
||
257 | */ |
||
258 | protected function resumeSession() |
||
267 | 12 | ||
268 | 12 | /** |
|
269 | 12 | * |
|
270 | 14 | * Sets the segment properties to $_SESSION references. |
|
271 | * |
||
272 | * @return null |
||
273 | * |
||
274 | */ |
||
275 | protected function load() |
||
289 | |||
290 | /** |
||
291 | * |
||
292 | * Resumes a previous session, or starts a new one, and loads the segment. |
||
293 | * |
||
294 | * @return null |
||
295 | * |
||
296 | */ |
||
297 | protected function resumeOrStartSession() |
||
304 | } |
||
305 |
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: