25 public static function getArray(
string $arrayKey): array {
27 $array = self::$sessionInstance->get ( $arrayKey );
28 if (! \is_array ( $array ))
44 $array = self::getArray ( $arrayKey );
45 $_SESSION [$arrayKey] = $array;
46 $search = \array_search ( $value, $array );
47 if ($search ===
false && $add) {
49 self::$sessionInstance->set ( $arrayKey, $array );
51 }
else if ($add !==
true) {
52 unset ( $array [$search] );
53 self::$sessionInstance->set ( $arrayKey, $array );
67 return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
78 return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
89 return self::$sessionInstance->set ( $key, UString::isBooleanTrue ( $value ) );
99 $v = self::$sessionInstance->get ( $key, false );
100 return UString::isBooleanTrue ( $v );
110 public static function session(
string $key, $default = NULL) {
111 return self::$sessionInstance->get ( $key, $default );
121 public static function get(
string $key, $default = NULL) {
122 return self::$sessionInstance->get ( $key, $default );
132 public static function set(
string $key, $value) {
133 return self::$sessionInstance->set ( $key, $value );
136 public static function setTmp(
string $key, $value, $duration) {
137 if (self::$sessionInstance->exists ( $key )) {
138 $object = self::$sessionInstance->get ( $key );
140 return $object->setValue ( $value );
144 return self::$sessionInstance->set ( $key, $object );
147 public static function getTmp(
string $key, $default =
null) {
148 if (self::$sessionInstance->exists ( $key )) {
149 $object = self::$sessionInstance->get ( $key );
151 $value = $object->getValue ();
152 if (isset ( $value )) {
153 return $object->getValue();
156 self::delete ( $key );
164 if (self::$sessionInstance->exists ( $key )) {
165 $object = self::$sessionInstance->get ( $key );
167 $value = $object->getTimeout ();
183 public static function delete(
string $key): void {
184 self::$sessionInstance->delete ( $key );
194 public static function inc(
string $key,
int $inc = 1) {
195 return self::set ( $key, self::get ( $key, 0 ) + $inc );
205 public static function dec(
string $key,
int $dec = 1) {
206 return self::set ( $key, self::get ( $key, 0 ) - $dec );
217 public static function concat(
string $key,
string $str,
string $default = NULL): string {
218 return self::set ( $key, self::get ( $key, $default ) . $str );
229 public static function apply(
string $key, $callback, $default = NULL) {
230 $value = self::get ( $key, $default );
231 if (\is_string ( $callback ) && \function_exists ( $callback )) {
232 $value = \call_user_func ( $callback, $value );
233 } elseif ($callback instanceof \Closure) {
234 $value = $callback ( $value );
238 return self::set ( $key, $value );
248 public static function Walk($callback, $userData =
null): array {
249 $all = self::$sessionInstance->getAll ();
250 foreach ( $all as $k => $v ) {
251 self::$sessionInstance->set ( $k, $callback ( $k, $v, $userData ) );
253 return self::$sessionInstance->getAll ();
262 public static function replace(array $keyAndValues): array {
263 foreach ( $keyAndValues as $k => $v ) {
264 self::$sessionInstance->set ( $k, $v );
266 return self::$sessionInstance->getAll ();
275 return self::$sessionInstance->getAll ();
283 public static function start($name =
null): void {
284 if (! isset ( self::$sessionInstance )) {
285 self::$sessionInstance = Startup::getSessionInstance ();
287 self::$sessionInstance->start ( $name );
296 return self::$sessionInstance->isStarted ();
305 public static function exists(
string $key): bool {
306 return self::$sessionInstance->exists ( $key );
316 public static function init(
string $key, $value) {
317 if (! self::$sessionInstance->exists ( $key )) {
318 self::$sessionInstance->set ( $key, $value );
320 return self::$sessionInstance->get ( $key );
327 self::$sessionInstance->terminate ();
336 if (isset ( self::$sessionInstance )) {
337 return \get_class ( self::$sessionInstance->getVerifyCsrf () );
347 if (isset ( self::$sessionInstance )) {
348 return \get_class ( self::$sessionInstance );
358 return self::$sessionInstance->visitorCount ();
366 public static function regenerateId(
bool $deleteOldSession=
false):bool {
367 return self::$sessionInstance->regenerateId($deleteOldSession);
Http Session utilities This class is part of Ubiquity.
static addValueToArray(string $arrayKey, $value)
Adds a value from an array in session.
static getTimeout(string $key)
static inc(string $key, int $inc=1)
Increment the value at the key index in session.
static terminate()
Terminates the active session.
static exists(string $key)
Returns true if the key exists in Session.
static concat(string $key, string $str, string $default=NULL)
Adds a string at the end of the value at the key index in session.
static addOrRemoveValueFromArray(string $arrayKey, $value, $add=null)
Adds or removes a value from an array in session.
static init(string $key, $value)
Initialize the key in Session if key does not exists.
static setTmp(string $key, $value, $duration)
static removeValueFromArray(string $arrayKey, $value)
Removes a value from an array in session.
static Walk($callback, $userData=null)
Apply a user supplied function to every member of Session array.
static getTmp(string $key, $default=null)
static setBoolean(string $key, $value)
Sets a boolean value at key position in session.
static apply(string $key, $callback, $default=NULL)
Applies a callback function to the value at the key index in session.
static getBoolean(string $key)
Returns a boolean stored at the key position in session.
static getCsrfProtectionClass()
Return the Csrf protection class name.
static getAll()
Returns the associative array of session vars.
static dec(string $key, int $dec=1)
Decrement the value at the key index in session.
static getArray(string $arrayKey)
Returns an array stored in session variable as $arrayKey.
static isStarted()
Returns true if the session is started.
static visitorCount()
Returns the number of sessions started.
static start($name=null)
Start new or resume existing session.
static regenerateId(bool $deleteOldSession=false)
Re-generates the session id.
static getInstanceClass()
Return the instance class name for the session.
static session(string $key, $default=NULL)
Returns the value stored at the key position in session.
static replace(array $keyAndValues)
Replaces elements from Session array with $keyAndValues.