Basic cookie jar implementation

This commit is contained in:
Dennis Eichhorn 2015-12-30 23:43:24 +01:00
parent a7d25081cd
commit 5365e39fc2

View File

@ -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']);
}
}
}