Ubiquity  2.0.2
php rapid development framework
USession.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
6 
13 class USession {
14  private static $name;
15 
23  public static function getArray($arrayKey) {
24  self::start ();
25  if (isset ( $_SESSION [$arrayKey] )) {
26  $array = $_SESSION [$arrayKey];
27  if (! is_array ( $array ))
28  $array = [ ];
29  } else
30  $array = [ ];
31  return $array;
32  }
33 
45  public static function addOrRemoveValueFromArray($arrayKey, $value, $add = true) {
46  $array = self::getArray ( $arrayKey );
47  $_SESSION [$arrayKey] = $array;
48  $search = array_search ( $value, $array );
49  if ($search === FALSE && $add) {
50  $_SESSION [$arrayKey] [] = $value;
51  return true;
52  } else {
53  unset ( $_SESSION [$arrayKey] [$search] );
54  $_SESSION [$arrayKey] = array_values ( $_SESSION [$arrayKey] );
55  return false;
56  }
57  }
58 
68  public static function removeValueFromArray($arrayKey, $value) {
69  return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
70  }
71 
81  public static function addValueToArray($arrayKey, $value) {
82  return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
83  }
84 
94  public static function setBoolean($key, $value) {
95  $_SESSION [$key] = UString::isBooleanTrue ( $value );
96  return $_SESSION [$key];
97  }
98 
106  public static function getBoolean($key) {
107  self::start ();
108  $ret = false;
109  if (isset ( $_SESSION [$key] )) {
110  $ret = UString::isBooleanTrue ( $_SESSION [$key] );
111  }
112  return $ret;
113  }
114 
124  public static function session($key, $default = NULL) {
125  self::start ();
126  return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
127  }
128 
138  public static function get($key, $default = NULL) {
139  self::start ();
140  return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
141  }
142 
150  public static function set($key, $value) {
151  $_SESSION [$key] = $value;
152  return $value;
153  }
154 
161  public static function delete($key) {
162  self::start ();
163  unset ( $_SESSION [$key] );
164  }
165 
173  public static function inc($key, $inc = 1) {
174  return self::set ( $key, self::get ( $key, 0 ) + $inc );
175  }
176 
184  public static function dec($key, $dec = 1) {
185  return self::set ( $key, self::get ( $key, 0 ) - $dec );
186  }
187 
195  public static function concat($key, $str, $default = NULL) {
196  return self::set ( $key, self::get ( $key, $default ) . $str );
197  }
198 
206  public static function apply($key, $callback, $default = NULL) {
207  $value = self::get ( $key, $default );
208  if (is_string ( $callback ) && function_exists ( $callback )) {
209  $value = call_user_func ( $callback, $value );
210  } elseif (is_callable ( $callback )) {
211  $value = $callback ( $value );
212  } else {
213  return $value;
214  }
215  return self::set ( $key, $value );
216  }
217 
225  public static function Walk($callback, $userData = null) {
226  self::start ();
227  array_walk ( $_SESSION, $callback, $userData );
228  return $_SESSION;
229  }
230 
237  public static function replace($keyAndValues) {
238  self::start ();
239  $_SESSION = array_replace ( $_SESSION, $keyAndValues );
240  return $_SESSION;
241  }
242 
248  public static function getAll() {
249  self::start ();
250  return $_SESSION;
251  }
252 
259  public static function start($name = null) {
260  if (! isset ( $_SESSION )) {
261  if (isset ( $name ) && $name !== "") {
262  self::$name = $name;
263  }
264  if (isset ( self::$name )) {
265  \session_name ( self::$name );
266  }
267  \session_start ();
268  }
269  }
270 
276  public static function isStarted() {
277  return isset ( $_SESSION );
278  }
279 
287  public static function exists($key) {
288  self::start ();
289  return isset ( $_SESSION [$key] );
290  }
291 
295  public static function terminate() {
296  if (! self::isStarted ())
297  return;
298  self::start ();
299  $_SESSION = array ();
300 
301  if (\ini_get ( "session.use_cookies" )) {
302  $params = \session_get_cookie_params ();
303  \setcookie ( \session_name (), '', \time () - 42000, $params ["path"], $params ["domain"], $params ["secure"], $params ["httponly"] );
304  }
305  \session_destroy ();
306  }
307 }
Http Session utilities.
Definition: USession.php:13
static dec($key, $dec=1)
Decrement the value at the key index in session.
Definition: USession.php:184
static exists($key)
Returns true if the key exists in Session.
Definition: USession.php:287
static session($key, $default=NULL)
Returns the value stored at the key position in session.
Definition: USession.php:124
static isStarted()
Returns true if the session is started.
Definition: USession.php:276
static apply($key, $callback, $default=NULL)
Applies a callback function to the value at the key index in session.
Definition: USession.php:206
static Walk($callback, $userData=null)
Apply a user supplied function to every member of Session array.
Definition: USession.php:225
static concat($key, $str, $default=NULL)
Adds a string at the end of the value at the key index in session.
Definition: USession.php:195
static getBoolean($key)
Returns a boolean stored at the key position in session.
Definition: USession.php:106
static replace($keyAndValues)
Replaces elements from Session array with $keyAndValues.
Definition: USession.php:237
static terminate()
Terminates the active session.
Definition: USession.php:295
static setBoolean($key, $value)
Sets a boolean value at key position in session.
Definition: USession.php:94
static getAll()
Returns the associative array of session vars.
Definition: USession.php:248
static start($name=null)
Start new or resume existing session.
Definition: USession.php:259
static inc($key, $inc=1)
Increment the value at the key index in session.
Definition: USession.php:173
static removeValueFromArray($arrayKey, $value)
Removes a value from an array in session.
Definition: USession.php:68
static getArray($arrayKey)
Returns an array stored in session variable as $arrayKey.
Definition: USession.php:23
static addValueToArray($arrayKey, $value)
Adds a value from an array in session.
Definition: USession.php:81
static addOrRemoveValueFromArray($arrayKey, $value, $add=true)
Adds or removes a value from an array in session.
Definition: USession.php:45