Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
USession.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\http;
4
8
16class USession {
17 protected static $sessionInstance;
18
25 public static function getArray(string $arrayKey): array {
26 if (self::$sessionInstance->exists ( $arrayKey )) {
27 $array = self::$sessionInstance->get ( $arrayKey );
28 if (! \is_array ( $array ))
29 $array = [ ];
30 } else
31 $array = [ ];
32 return $array;
33 }
34
43 public static function addOrRemoveValueFromArray(string $arrayKey, $value, $add = null): ?bool {
44 $array = self::getArray ( $arrayKey );
45 $_SESSION [$arrayKey] = $array;
46 $search = \array_search ( $value, $array );
47 if ($search === false && $add) {
48 $array [] = $value;
49 self::$sessionInstance->set ( $arrayKey, $array );
50 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
66 public static function removeValueFromArray(string $arrayKey, $value): ?bool {
67 return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
68 }
69
77 public static function addValueToArray(string $arrayKey, $value): ?bool {
78 return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
79 }
80
88 public static function setBoolean(string $key, $value) {
89 return self::$sessionInstance->set ( $key, UString::isBooleanTrue ( $value ) );
90 }
91
98 public static function getBoolean(string $key): bool {
99 $v = self::$sessionInstance->get ( $key, false );
100 return UString::isBooleanTrue ( $v );
101 }
102
110 public static function session(string $key, $default = NULL) {
111 return self::$sessionInstance->get ( $key, $default );
112 }
113
121 public static function get(string $key, $default = NULL) {
122 return self::$sessionInstance->get ( $key, $default );
123 }
124
132 public static function set(string $key, $value) {
133 return self::$sessionInstance->set ( $key, $value );
134 }
135
136 public static function setTmp(string $key, $value, $duration) {
137 if (self::$sessionInstance->exists ( $key )) {
138 $object = self::$sessionInstance->get ( $key );
139 if ($object instanceof SessionObject) {
140 return $object->setValue ( $value );
141 }
142 }
143 $object = new SessionObject ( $value, $duration );
144 return self::$sessionInstance->set ( $key, $object );
145 }
146
147 public static function getTmp(string $key, $default = null) {
148 if (self::$sessionInstance->exists ( $key )) {
149 $object = self::$sessionInstance->get ( $key );
150 if ($object instanceof SessionObject) {
151 $value = $object->getValue ();
152 if (isset ( $value )) {
153 return $object->getValue();
154 }
155 else {
156 self::delete ( $key );
157 }
158 }
159 }
160 return $default;
161 }
162
163 public static function getTimeout(string $key) {
164 if (self::$sessionInstance->exists ( $key )) {
165 $object = self::$sessionInstance->get ( $key );
166 if ($object instanceof SessionObject) {
167 $value = $object->getTimeout ();
168 if ($value < 0) {
169 return 0;
170 } else {
171 return $value;
172 }
173 }
174 }
175 return;
176 }
177
183 public static function delete(string $key): void {
184 self::$sessionInstance->delete ( $key );
185 }
186
194 public static function inc(string $key, int $inc = 1) {
195 return self::set ( $key, self::get ( $key, 0 ) + $inc );
196 }
197
205 public static function dec(string $key, int $dec = 1) {
206 return self::set ( $key, self::get ( $key, 0 ) - $dec );
207 }
208
217 public static function concat(string $key, string $str, string $default = NULL): string {
218 return self::set ( $key, self::get ( $key, $default ) . $str );
219 }
220
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 );
235 } else {
236 return $value;
237 }
238 return self::set ( $key, $value );
239 }
240
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 ) );
252 }
253 return self::$sessionInstance->getAll ();
254 }
255
262 public static function replace(array $keyAndValues): array {
263 foreach ( $keyAndValues as $k => $v ) {
264 self::$sessionInstance->set ( $k, $v );
265 }
266 return self::$sessionInstance->getAll ();
267 }
268
274 public static function getAll(): array {
275 return self::$sessionInstance->getAll ();
276 }
277
283 public static function start($name = null): void {
284 if (! isset ( self::$sessionInstance )) {
285 self::$sessionInstance = Startup::getSessionInstance ();
286 }
287 self::$sessionInstance->start ( $name );
288 }
289
295 public static function isStarted(): bool {
296 return self::$sessionInstance->isStarted ();
297 }
298
305 public static function exists(string $key): bool {
306 return self::$sessionInstance->exists ( $key );
307 }
308
316 public static function init(string $key, $value) {
317 if (! self::$sessionInstance->exists ( $key )) {
318 self::$sessionInstance->set ( $key, $value );
319 }
320 return self::$sessionInstance->get ( $key );
321 }
322
326 public static function terminate(): void {
327 self::$sessionInstance->terminate ();
328 }
329
335 public static function getCsrfProtectionClass() {
336 if (isset ( self::$sessionInstance )) {
337 return \get_class ( self::$sessionInstance->getVerifyCsrf () );
338 }
339 }
340
346 public static function getInstanceClass() {
347 if (isset ( self::$sessionInstance )) {
348 return \get_class ( self::$sessionInstance );
349 }
350 }
351
357 public static function visitorCount() {
358 return self::$sessionInstance->visitorCount ();
359 }
360
366 public static function regenerateId(bool $deleteOldSession=false):bool {
367 return self::$sessionInstance->regenerateId($deleteOldSession);
368 }
369}
Starts the framework.
Definition Startup.php:19
String utilities.
Definition UString.php:15
Http Session utilities This class is part of Ubiquity.
Definition USession.php:16
static addValueToArray(string $arrayKey, $value)
Adds a value from an array in session.
Definition USession.php:77
static getTimeout(string $key)
Definition USession.php:163
static inc(string $key, int $inc=1)
Increment the value at the key index in session.
Definition USession.php:194
static terminate()
Terminates the active session.
Definition USession.php:326
static exists(string $key)
Returns true if the key exists in Session.
Definition USession.php:305
static concat(string $key, string $str, string $default=NULL)
Adds a string at the end of the value at the key index in session.
Definition USession.php:217
static addOrRemoveValueFromArray(string $arrayKey, $value, $add=null)
Adds or removes a value from an array in session.
Definition USession.php:43
static init(string $key, $value)
Initialize the key in Session if key does not exists.
Definition USession.php:316
static setTmp(string $key, $value, $duration)
Definition USession.php:136
static removeValueFromArray(string $arrayKey, $value)
Removes a value from an array in session.
Definition USession.php:66
static Walk($callback, $userData=null)
Apply a user supplied function to every member of Session array.
Definition USession.php:248
static getTmp(string $key, $default=null)
Definition USession.php:147
static setBoolean(string $key, $value)
Sets a boolean value at key position in session.
Definition USession.php:88
static apply(string $key, $callback, $default=NULL)
Applies a callback function to the value at the key index in session.
Definition USession.php:229
static getBoolean(string $key)
Returns a boolean stored at the key position in session.
Definition USession.php:98
static getCsrfProtectionClass()
Return the Csrf protection class name.
Definition USession.php:335
static getAll()
Returns the associative array of session vars.
Definition USession.php:274
static dec(string $key, int $dec=1)
Decrement the value at the key index in session.
Definition USession.php:205
static getArray(string $arrayKey)
Returns an array stored in session variable as $arrayKey.
Definition USession.php:25
static isStarted()
Returns true if the session is started.
Definition USession.php:295
static visitorCount()
Returns the number of sessions started.
Definition USession.php:357
static start($name=null)
Start new or resume existing session.
Definition USession.php:283
static regenerateId(bool $deleteOldSession=false)
Re-generates the session id.
Definition USession.php:366
static getInstanceClass()
Return the instance class name for the session.
Definition USession.php:346
static session(string $key, $default=NULL)
Returns the value stored at the key position in session.
Definition USession.php:110
static replace(array $keyAndValues)
Replaces elements from Session array with $keyAndValues.
Definition USession.php:262