<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 1/18/2017
 * Time: 10:25 AM
 */

namespace a_modules\common\repository;


class Session
{
    /**
     * @return string
     */
    public static function get_session_id(){
        return session_id();
    }

    /**
     * @return array
     */
    public static function all(){
        return $_SESSION;
    }

    /**
     * Get a specific value from $_SESSION referenced by key
     * @param $key
     * @return array
     */
    public static function get($key){
        return isset($_SESSION[static::get_session_id()][$key]) ? $_SESSION[static::get_session_id()][$key] : null;
    }

    /**
     * @param $key
     * @return mixed | null
     */
    public static function gget($key){
        return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
    }

    /**
     * Set a specific value in $_SESSION referenced by key
     * @param string $key
     * @param mixed $value
     * @return mixed $value
     */
    public static function set($key, $value){
        $_SESSION[static::get_session_id()][$key] = $value;
        return $value;
    }

    /**
     * @param $key
     * @param $value
     * @return mixed
     */
    public static function gset($key, $value){
        $_SESSION[$key] = $value;
        return $value;
    }

    public static function has($key) {
        return isset($_SESSION[static::get_session_id()][$key]);
    }

    public static function ghas($key) {
        return isset($_SESSION[$key]);
    }

    public static function delete($key) {
        unset($_SESSION[static::get_session_id()][$key]);
    }

    public static function gdelete($key) {
        unset($_SESSION[$key]);
    }




    public static function logout() {
        $s = collect($_SESSION)->toArray();

        $admin_info = array();
        if(isset($s['user_id'])) {
            $admin_info = array(
                'user_id' => $s['user_id'],
                'user_fulname' => $s['user_fulname']
            );
        }
        $_SESSION = array();
        session_regenerate_id(true);
        $sid = static::get_session_id();
        $_SESSION['sid'] = $sid;
        $_SESSION = array_merge($_SESSION, $admin_info);


    }
}