<?php
 class Cookie {
 public static function set(string $name, string $v, $time = 86400, string $path = null) {
 $expire = time() + $time;
 if(is_object($v)) {
 if($v instanceof \TCMS\Model) {
 $stdClass = new \stdClass();
 $fields = $v->getFields();
 foreach($fields as $k => $f) {
 $stdClass->$k = $f->value;
 }
 $v = json_encode($stdClass);
 }
 else {
 $v = json_encode($v);
 }
 }
 else if(is_array($v)) {
 $v = json_encode($v);
 }
 else if(!is_string($v)) {
 $v = $v . '';
 }
 if (empty($path)) setcookie($name, $v, $expire, '/');
 else setcookie($name, $v, $expire, $path);
 $_COOKIE[$name] = $v;
 }
 public static function has($name = 'cookie') {
 return isset($_COOKIE[$name]);
 }
 public static function get($name = 'cookie') {
 if (isset($_COOKIE[$name])) {
 return $_COOKIE[$name];
 }
 return null;
 }
 public static function remove($name = 'cookie', $path = null) {
 if (isset($_COOKIE[$name])) {
 unset($_COOKIE[$name]);
 if (empty($path)) setcookie($name, null, -1, '/');
 else setcookie($name, null, -1, $path);
 }
 }
 public static function removeAll() {
 if (isset($_SERVER['HTTP_COOKIE'])) {
 $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
 foreach ($cookies as $cookie) {
 $parts = explode('=', $cookie);
 $name = trim($parts[0]);
 setcookie($name, '', time() - 1000, '/');
 }
 }
 }
 }
 ?>