Skip to content

Commit 7f6ef35

Browse files
committed
Added flash message and start options
1 parent 40bfac3 commit 7f6ef35

1 file changed

Lines changed: 81 additions & 20 deletions

File tree

src/Session.php

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,92 @@
22

33
namespace Mk4U\Http;
44

5-
use RuntimeException;
5+
use Mk4U\Http\Session\Flash;
66

77
/**
88
* Session class
99
*/
1010
class Session
1111
{
12+
private const CFG = [
13+
"auto_start" => "boolean",
14+
"cache_expire" => "integer",
15+
"cache_limiter" => "string",
16+
"cookie_domain" => "string",
17+
"cookie_httponly" => "boolean",
18+
"cookie_lifetime" => "integer",
19+
"cookie_path" => "string ",
20+
"cookie_samesite" => "string",
21+
"cookie_secure" => "boolean",
22+
"gc_divisor" => "integer",
23+
"gc_maxlifetime" => "integer",
24+
"gc_probability" => "integer",
25+
"lazy_write" => "boolean",
26+
"name" => "string",
27+
"referer_check" => "string",
28+
"save_handler" => "string",
29+
"save_path" => "string",
30+
"serialize_handler" => "string",
31+
"sid_bits_per_character" => "integer",
32+
"sid_length" => "integer",
33+
"trans_sid_hosts" => "string",
34+
"trans_sid_tags" => "string",
35+
"use_cookies" => "boolean",
36+
"use_only_cookies" => "boolean",
37+
"use_strict_mode" => "boolean",
38+
"use_trans_sid " => "boolean",
39+
];
40+
41+
use Flash;
42+
1243
/**
1344
* Inicializa la session
1445
**/
15-
public static function start()
46+
public static function start(array $options = []): bool
1647
{
1748
if (session_status() !== PHP_SESSION_ACTIVE) {
49+
if (!empty($options)) {
50+
self::validate($options);
51+
return session_start($options);
52+
}
53+
1854
return session_start([
1955
"name" => "_mk4u_",
56+
"use_cookies" => true,
2057
"use_only_cookies" => true,
2158
"cookie_lifetime" => 0,
22-
"cookie_httponly"=> true,
23-
"cookie_secure"=>true,
59+
"cookie_httponly" => true,
60+
"cookie_secure" => true,
2461
"use_strict_mode" => true,
25-
//"delete_old_session"=>true,
26-
//'read_and_close' => true,
2762
]);
2863
}
64+
return false;
2965
}
3066

3167
/**
3268
* Devuelve el valor de $_SESSION
3369
*/
34-
public static function get(?string $name = null,mixed $default=null): mixed
70+
public static function get(?string $name = null, mixed $default = null): mixed
3571
{
3672
if (is_null($name)) {
3773
return $_SESSION;
3874
}
3975

40-
if (isset($default)) {
41-
return $default;
42-
} else {
43-
if (self::has($name) === false) {
44-
throw new RuntimeException(sprintf("The session '%s' does not exist", $name));
45-
}
46-
76+
if (self::has($name)) {
4777
return $_SESSION[$name];
78+
} else {
79+
return $default;
4880
}
49-
5081
}
5182

5283
/**
5384
* Establece valores para $_SESSION
5485
*
55-
* en caso de existir la session sobreescribe el valor
86+
* En caso de existir la session sobreescribe el valor
5687
*/
5788
public static function set(string $name, mixed $value): void
5889
{
59-
$_SESSION[$name]=$value;
90+
$_SESSION[$name] = $value;
6091
}
6192

6293
/**
@@ -78,15 +109,15 @@ public static function has(string $name): bool
78109
/**
79110
* Genera un nuevo ID de session
80111
*/
81-
public static function renewId() : void
112+
public static function renewId(): void
82113
{
83114
session_regenerate_id();
84115
}
85116

86117
/**
87118
* Destruye la sesion con todos sus datos
88119
*/
89-
public static function destroy() : void
120+
public static function destroy(): void
90121
{
91122
session_unset();
92123
session_destroy();
@@ -95,8 +126,38 @@ public static function destroy() : void
95126
/**
96127
* Devuelve el id de la session
97128
*/
98-
public static function id() : string
129+
public static function id(): string
99130
{
100131
return session_id();
101132
}
133+
134+
/**
135+
* Validar opciones de inicio
136+
*/
137+
private static function validate(array $options): void
138+
{
139+
foreach ($options as $key => $value) {
140+
if (!array_key_exists($key, self::CFG)) {
141+
throw new \RuntimeException(sprintf("'%s' is not a valid configuration parameter.", $key));
142+
}
143+
144+
$expectedType = self::CFG[$key];
145+
if (gettype($value) !== $expectedType) {
146+
147+
throw new \RuntimeException(sprintf("Expected data type '%s' for '%s,", $expectedType, $key));
148+
}
149+
}
150+
}
151+
152+
// -------------- Flash Messages ---------------
153+
/**
154+
* Establece y muestra los flash message
155+
*/
156+
public static function flash(string $name, mixed $value=null): ?string
157+
{
158+
if (empty($value)) {
159+
return static::getflash($name);
160+
}
161+
return static::setflash($name, $value);
162+
}
102163
}

0 commit comments

Comments
 (0)