diff --git a/DataStorage/Cookie/CookieJar.php b/DataStorage/Cookie/CookieJar.php index e4a156e69..eb50f5325 100644 --- a/DataStorage/Cookie/CookieJar.php +++ b/DataStorage/Cookie/CookieJar.php @@ -26,4 +26,52 @@ namespace phpOMS\Cookie; */ class CookieJar { + private $cookies = []; + + public function __construct() + { + $this->cookies = $_COOKIE; + } + + public function set($id, $value, \int $expiry = 86400, $path = '/', $domain = null, \bool $secure = false, \bool $httponly = true, \bool $overwrite = true) : \bool + { + if ($overwrite || !isset($this->cookies[$id])) { + $this->cookies[$id] = [ + 'value' => $value, + 'expiry' => $expiry, + 'path' => $path, + 'domain' => $domain, + 'secure' => $secure, + 'httponly' => $httponly, + ]; + + return true; + } + + return false; + } + + public function remove($id) : \bool + { + if (isset($this->cookies[$id])) { + unset($this->cookies[$id]); + + return true; + } + + return false; + } + + public function delete($id) : \bool + { + $this->remove($id); + setcookie($id, '', time() - 3600); + } + + public function save() + { + foreach ($this->cookies as $key => $cookie) { + setcookie($key, $cookie['value'], $cookie['expiry'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']); + } + } }