<?php
// namespace core;

/**
* Cookie handle class
*/
class Cookie{
	protected $name = "";
	protected $value = "";
	protected $expires = COOKIE_EXPIRES;
	protected $path = COOKIE_PATH;
	protected $domain = COOKIE_DOMAIN;
	protected $secure = COOKIE_SECURE;
	protected $httpOnly = COOKIE_HTTPONLY;

	/**
	* Returns cookie value or false
	* @param STRING $cookieName clean cookie name
	* @return STRING returns the sanitized value or empty string
	*/
	public static function get_cookie_value($cookieName=null){
		if(empty($_COOKIE[$cookieName])){
			return '';
		}
		return filter_var($_COOKIE[$cookieName],FILTER_SANITIZE_STRING);
	}

	/**
	* Sets cookie value under name, this does not indicate whether the user accepted the cookie
	* @param STRING $cookieName clean cookie name
	* @return BOOL true on success else false
	*/
	public static function set_cookie($cookieName=null,$cookieVale=null){
		if($cookieName==null OR $cookieVale==null){
			return false;
		}
		return setcookie($cookieName,$cookieVale,time() + COOKIE_EXPIRES,COOKIE_PATH,COOKIE_DOMAIN,COOKIE_SECURE,COOKIE_HTTPONLY);
	}

	/**
	* Deletes cookie under name, this does not indicate whether the user accepted the cookie
	* @param STRING $cookieName clean cookie name
	* @return BOOL true on success else false
	*/
	public static function del_cookie($cookieName=null){
		if($cookieName==null){
			return false;
		}
		return setcookie($cookieName,"",time() - COOKIE_EXPIRES,COOKIE_PATH,COOKIE_DOMAIN,COOKIE_SECURE,COOKIE_HTTPONLY);
	}

	/**
	* Gets all cookies under defined directory and that are not multiple
	* @return ARRAY of sanitized cookie or empty one
	*/
	public static function get_cookies(){
		if(!isset($_COOKIE)){
			return array();
		}
		$sanitized = array();
		foreach ($_COOKIE as $name => $value) {
			$sanitized[filter_var($name,FILTER_SANITIZE_STRING)] = filter_var($value,FILTER_SANITIZE_STRING);
		}
		return $sanitized;
	}
  
	/**
	* Gets all cookies under defined directory that are multiple optionally with limit
	* @param STRING $cookieName clean cookie name
	* @param STRING $cookieLimit define for any of retrieve number
	* @return ARRAY of sanitized cookie or empty one
	*/
	public static function get_multi_cookie($cookieName=null,$cookieLimit=null){
		if(!isset($_COOKIE[$cookieName]) OR $cookieName==null){
			return array();
		}
		$sanitized;
		$breakLimit = count($_COOKIE[$cookieName]);
		foreach ($_COOKIE[$cookieName] as $name => $value) {
			// var_dump($_COOKIE[$cookieName]);
			$sanitized[filter_var($name,FILTER_SANITIZE_STRING)] = filter_var($value,FILTER_SANITIZE_STRING);
			if($cookieLimit == $breakLimit)
				break;
		}
		// var_dump($sanitized);
		return $sanitized;
	}

	public function getPath() {
		return $this->path;
	}

	public function setPath(string $path) {
		$this->path = $path;
	}

}