Passed
Branch master (7dd754)
by Jean-Christophe
16:28
created

USession::getArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\utils\http\session\SessionObject;
7
use Ubiquity\controllers\Startup;
8
9
/**
10
 * Http Session utilities
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.1.1
15
 */
16
class USession {
17
	protected static $sessionInstance;
18
19
	/**
20
	 * Returns an array stored in session variable as $arrayKey
21
	 *
22
	 * @param string $arrayKey the key of the array to return
23
	 * @return array
24
	 */
25 5
	public static function getArray($arrayKey): array {
26 5
		if (self::$sessionInstance->exists ( $arrayKey )) {
27 1
			$array = self::$sessionInstance->get ( $arrayKey );
28 1
			if (! \is_array ( $array ))
29 1
				$array = [ ];
30
		} else
31 4
			$array = [ ];
32 5
		return $array;
33
	}
34
35
	/**
36
	 * Adds or removes a value from an array in session
37
	 *
38
	 * @param string $arrayKey the key of the array to add or remove in
39
	 * @param mixed $value the value to add
40
	 * @param boolean|null $add If true, adds otherwise removes
41
	 * @return boolean|null
42
	 */
43 5
	public static function addOrRemoveValueFromArray($arrayKey, $value, $add = null): ?bool {
44 5
		$array = self::getArray ( $arrayKey );
45 5
		$_SESSION [$arrayKey] = $array;
46 5
		$search = \array_search ( $value, $array );
47 5
		if ($search === false && $add) {
48 5
			$array [] = $value;
49 5
			self::$sessionInstance->set ( $arrayKey, $array );
50 5
			return true;
51
		} else if ($add !== true) {
52
			unset ( $array [$search] );
53
			self::$sessionInstance->set ( $arrayKey, $array );
54
			return false;
55
		}
56
		return null;
57
	}
58
59
	/**
60
	 * Removes a value from an array in session
61
	 *
62
	 * @param string $arrayKey the key of the array to remove in
63
	 * @param mixed $value the value to remove
64
	 * @return boolean|null
65
	 */
66
	public static function removeValueFromArray($arrayKey, $value): ?bool {
67
		return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
68
	}
69
70
	/**
71
	 * Adds a value from an array in session
72
	 *
73
	 * @param string $arrayKey the key of the array to add in
74
	 * @param mixed $value the value to add
75
	 * @return boolean|null
76
	 */
77
	public static function addValueToArray($arrayKey, $value): ?bool {
78
		return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
79
	}
80
81
	/**
82
	 * Sets a boolean value at key position in session
83
	 *
84
	 * @param string $key the key to add or set in
85
	 * @param mixed $value the value to set
86
	 * @return boolean
87
	 */
88
	public static function setBoolean($key, $value) {
89
		return self::$sessionInstance->set ( $key, UString::isBooleanTrue ( $value ) );
90
	}
91
92
	/**
93
	 * Returns a boolean stored at the key position in session
94
	 *
95
	 * @param string $key the key to add or set
96
	 * @return boolean
97
	 */
98
	public static function getBoolean($key): bool {
99
		$v = self::$sessionInstance->get ( $key, false );
100
		return UString::isBooleanTrue ( $v );
101
	}
102
103
	/**
104
	 * Returns the value stored at the key position in session
105
	 *
106
	 * @param string $key the key to retreive
107
	 * @param mixed $default the default value to return if the key does not exists in session
108
	 * @return mixed
109
	 */
110 1
	public static function session($key, $default = NULL) {
111 1
		return self::$sessionInstance->get ( $key, $default );
112
	}
113
114
	/**
115
	 * Returns the value stored at the key position in session
116
	 *
117
	 * @param string $key the key to retreive
118
	 * @param mixed $default the default value to return if the key does not exists in session
119
	 * @return mixed
120
	 */
121 10
	public static function get($key, $default = NULL) {
122 10
		return self::$sessionInstance->get ( $key, $default );
123
	}
124
125
	/**
126
	 * Adds or sets a value to the Session at position $key
127
	 *
128
	 * @param string $key the key to add or set
129
	 * @param mixed $value
130
	 */
131 9
	public static function set($key, $value) {
132 9
		return self::$sessionInstance->set ( $key, $value );
133
	}
134
135 1
	public static function setTmp($key, $value, $duration) {
136 1
		if (self::$sessionInstance->exists ( $key )) {
137
			$object = self::$sessionInstance->get ( $key );
138
			if ($object instanceof SessionObject) {
139
				return $object->setValue ( $value );
140
			}
141
		}
142 1
		$object = new SessionObject ( $value, $duration );
143 1
		return self::$sessionInstance->set ( $key, $object );
144
	}
145
146 1
	public static function getTmp($key, $default = null) {
147 1
		if (self::$sessionInstance->exists ( $key )) {
148 1
			$object = self::$sessionInstance->get ( $key );
149 1
			if ($object instanceof SessionObject) {
150 1
				$value = $object->getValue ();
151 1
				if (isset ( $value ))
152 1
					return $object->getValue ();
153
				else {
154 1
					self::delete ( $key );
155
				}
156
			}
157
		}
158 1
		return $default;
159
	}
160
161
	public static function getTimeout($key) {
162
		if (self::$sessionInstance->exists ( $key )) {
163
			$object = self::$sessionInstance->get ( $key );
164
			if ($object instanceof SessionObject) {
165
				$value = $object->getTimeout ();
166
				if ($value < 0) {
167
					return 0;
168
				} else {
169
					return $value;
170
				}
171
			}
172
		}
173
		return;
174
	}
175
176
	/**
177
	 * Deletes the key in Session
178
	 *
179
	 * @param string $key the key to delete
180
	 */
181 3
	public static function delete($key): void {
182 3
		self::$sessionInstance->delete ( $key );
183 3
	}
184
185
	/**
186
	 * Increment the value at the key index in session
187
	 *
188
	 * @param string $key
189
	 * @param number $inc
190
	 * @return number
191
	 */
192 1
	public static function inc($key, $inc = 1) {
193 1
		return self::set ( $key, self::get ( $key, 0 ) + $inc );
194
	}
195
196
	/**
197
	 * Decrement the value at the key index in session
198
	 *
199
	 * @param string $key
200
	 * @param number $dec
201
	 * @return number
202
	 */
203 1
	public static function dec($key, $dec = 1) {
204 1
		return self::set ( $key, self::get ( $key, 0 ) - $dec );
205
	}
206
207
	/**
208
	 * Adds a string at the end of the value at the key index in session
209
	 *
210
	 * @param string $key
211
	 * @param string $str
212
	 * @return string
213
	 */
214
	public static function concat($key, $str, $default = NULL): string {
215
		return self::set ( $key, self::get ( $key, $default ) . $str );
216
	}
217
218
	/**
219
	 * Applies a callback function to the value at the key index in session
220
	 *
221
	 * @param string $key
222
	 * @param string|callable $callback
223
	 * @return mixed
224
	 */
225 1
	public static function apply($key, $callback, $default = NULL) {
226 1
		$value = self::get ( $key, $default );
227 1
		if (\is_string ( $callback ) && \function_exists ( $callback )) {
228 1
			$value = \call_user_func ( $callback, $value );
229 1
		} elseif ($callback instanceof \Closure) {
230 1
			$value = $callback ( $value );
231
		} else {
232
			return $value;
233
		}
234 1
		return self::set ( $key, $value );
235
	}
236
237
	/**
238
	 * Apply a user supplied function to every member of Session array
239
	 *
240
	 * @param callable $callback
241
	 * @param mixed $userData
242
	 * @return array
243
	 */
244
	public static function Walk($callback, $userData = null): array {
245
		$all = self::$sessionInstance->getAll ();
246
		foreach ( $all as $k => $v ) {
247
			self::$sessionInstance->set ( $k, $callback ( $k, $v, $userData ) );
248
		}
249
		return self::$sessionInstance->getAll ();
250
	}
251
252
	/**
253
	 * Replaces elements from Session array with $keyAndValues
254
	 *
255
	 * @param array $keyAndValues
256
	 * @return array
257
	 */
258
	public static function replace($keyAndValues): array {
259
		foreach ( $keyAndValues as $k => $v ) {
260
			self::$sessionInstance->set ( $k, $v );
261
		}
262
		return self::$sessionInstance->getAll ();
263
	}
264
265
	/**
266
	 * Returns the associative array of session vars
267
	 *
268
	 * @return array
269
	 */
270
	public static function getAll(): array {
271
		return self::$sessionInstance->getAll ();
272
	}
273
274
	/**
275
	 * Start new or resume existing session
276
	 *
277
	 * @param string|null $name the name of the session
278
	 */
279 59
	public static function start($name = null): void {
280 59
		if (! isset ( self::$sessionInstance )) {
281 35
			self::$sessionInstance = Startup::getSessionInstance ();
282
		}
283 59
		self::$sessionInstance->start ( $name );
284 59
	}
285
286
	/**
287
	 * Returns true if the session is started
288
	 *
289
	 * @return boolean
290
	 */
291
	public static function isStarted(): bool {
292
		return self::$sessionInstance->isStarted ();
293
	}
294
295
	/**
296
	 * Returns true if the key exists in Session
297
	 *
298
	 * @param string $key the key to test
299
	 * @return boolean
300
	 */
301 14
	public static function exists($key): bool {
302 14
		return self::$sessionInstance->exists ( $key );
303
	}
304
305
	/**
306
	 * Initialize the key in Session if key does not exists
307
	 *
308
	 * @param string $key
309
	 * @param mixed $value
310
	 * @return mixed
311
	 */
312 1
	public static function init($key, $value) {
313 1
		if (! self::$sessionInstance->exists ( $key )) {
314 1
			self::$sessionInstance->set ( $key, $value );
315
		}
316 1
		return self::$sessionInstance->get ( $key );
317
	}
318
319
	/**
320
	 * Terminates the active session
321
	 */
322 2
	public static function terminate(): void {
323 2
		self::$sessionInstance->terminate ();
324 2
	}
325
}
326