2015-11-30 21:16:17 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
|
|
|
class SessionService {
|
|
|
|
/**
|
|
|
|
* Get value of $key in $section, or $defaultValue
|
|
|
|
*
|
|
|
|
* @param $section
|
|
|
|
* @param $key
|
|
|
|
* @param null $defaultValue
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($section, $key, $defaultValue=null) {
|
|
|
|
assert(!empty($key));
|
|
|
|
assert(!empty($section));
|
|
|
|
|
|
|
|
$this->initSectionIfNeeded($section);
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
return $_SESSION[$section][$key] ?? $defaultValue;
|
2015-11-30 21:16:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a $value for $key in $section
|
|
|
|
*
|
|
|
|
* @param $section
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function set($section, $key, $value): void
|
|
|
|
{
|
2015-11-30 21:16:17 +01:00
|
|
|
assert(!empty($key));
|
|
|
|
assert(!empty($section));
|
|
|
|
|
|
|
|
$this->initSectionIfNeeded($section);
|
|
|
|
|
|
|
|
$_SESSION[$section][$key] = $value;
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:38:00 +02:00
|
|
|
/**
|
|
|
|
* Remove a session value
|
|
|
|
*
|
|
|
|
* @param $section
|
|
|
|
* @param $key
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function remove($section, $key): void
|
|
|
|
{
|
2016-04-29 19:38:00 +02:00
|
|
|
assert(!empty($key));
|
|
|
|
assert(!empty($section));
|
|
|
|
|
|
|
|
unset($_SESSION[$section][$key]);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
private function initSectionIfNeeded($section): void
|
|
|
|
{
|
2015-11-30 21:16:17 +01:00
|
|
|
if (!isset($_SESSION[$section])) {
|
2018-02-19 00:18:43 +01:00
|
|
|
$_SESSION[$section] = [];
|
2015-11-30 21:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-20 17:46:50 +01:00
|
|
|
}
|