Init
This commit is contained in:
57
src/Session/Session.php
Normal file
57
src/Session/Session.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Session\Session;
|
||||
|
||||
class Session implements SessionInterface {
|
||||
|
||||
const INT = "INT";
|
||||
const STRING = "STRING";
|
||||
|
||||
public function __construct(string $name = "usi") {
|
||||
if(session_status() == PHP_SESSION_NONE) {
|
||||
if(in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)){
|
||||
session_id("cli");
|
||||
} elseif(!empty($name)) {
|
||||
session_name($name);
|
||||
}
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function has(string $name = "") : bool {
|
||||
return (!empty($name)) ? isset($this->getSession()[$name]) : !empty($this->getSession());
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function get(string $name = "", string $type = ""): mixed {
|
||||
$session = $this->getSession();
|
||||
if(!empty($name = strtolower($name))) {
|
||||
if (!isset($session[$name])) return null;
|
||||
elseif ($type == self::STRING) {
|
||||
return (!empty($session[$name])) ? (string)$session[$name] : null;
|
||||
}
|
||||
elseif ($type == self::INT) {
|
||||
return (!empty((int)$session[$name]) || $session[$name]==0) ? (int)$session[$name] : null;
|
||||
}
|
||||
return $session[$name];
|
||||
}
|
||||
return $session;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function set(string $name, $value = null) : void {
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function clear(string $name = null) : void {
|
||||
if (isset($name)) unset($_SESSION[$name]);
|
||||
else $_SESSION = [];
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
private function getSession() : array {
|
||||
return $_SESSION;
|
||||
}
|
||||
}
|
||||
38
src/Session/SessionInterface.php
Normal file
38
src/Session/SessionInterface.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 10.10.2023
|
||||
* Time: 14:34
|
||||
*/
|
||||
|
||||
namespace Rmphp\Session\Session;
|
||||
|
||||
interface SessionInterface {
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name = "") : bool;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $type
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name = "", string $type = ""): mixed;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $value
|
||||
*/
|
||||
public function set(string $name, $value = null) : void;
|
||||
|
||||
/**
|
||||
* @param string|null $name
|
||||
* @return void
|
||||
*/
|
||||
public function clear(string $name = null) : void;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user