<?php
// namespace helpers;

/**
* Session Class - prefix sessions with basic methods
**/
class Session{

	/* Determine if session has started */
	private static $_sessionStarted = false;

	/**
	* If session has not started, start sessions
	*/
	public static function init(){
		if(self::$_sessionStarted == false){
			session_start();
			self::$_sessionStarted = true;
			/* set the default template */
			// Session::set('template','default');
		}
	}

	/**
	* Add value to a session
	* @param STRING $key name the data to save
	* @param STRING $value the data to save
	*/
	public static function set($key,$value = false){
		/* Check whether session is set in array or not, if array then set all session key-values in loop */
		if(is_array($key) && $value === false){
			foreach ($key as $name => $value) {
				$_SESSION[SESSION_PREFIX.$name] = $value;
			}
		} else {
			$_SESSION[SESSION_PREFIX.$key] = $value;
		}
	}

	/**
	* Get requested item from session
	* @param STRING $key item to look for in session
	* @param BOOL $secondkey if used then use as a second key
	* @return STRING returns the key
	*/
	public static function get($key,$secondkey = false){
		if($secondkey == true){
			if(isset($_SESSION[SESSION_PREFIX.$key][$secondkey])){
				return $_SESSION[SESSION_PREFIX.$key][$secondkey];
			}
		}else{
			if(isset($_SESSION[SESSION_PREFIX.$key])){
				return $_SESSION[SESSION_PREFIX.$key];
			}
		}
		return false;
	}

	/**
	* Extract item from session then delete from the session, finally return the extracted item
	* @param STRING $key item to extract
	* @return STRING return item
	*/
	public static function pull($key){
		$value = $_SESSION[SESSION_PREFIX.$key];
		unset($_SESSION[SESSION_PREFIX.$key]);
		return $value;
	}

	/**
	* Return current session id... LOL
	* @return STRING with the session id.
	*/
	public static function id(){
		return session_id();
	}

	/**
	* Return the session array
	* @return ARRAY of session indexes
	*/
	public static function display(){
		return $_SESSION;
	}

	/**
	* Empties/unset the session but not deleting it totally
	*/
	public static function destroy($key='') {
		if(self::$_sessionStarted == true) {
			if(empty($key)) {
				session_unset();
			} else {
				unset($_SESSION[SESSION_PREFIX.$key]);
			}
		}
	}
}