From 9bf167f782a042e2bea24cc55be8fe9ee5504635 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 14 Sep 2023 15:25:50 +0000 Subject: [PATCH] - Two bugfixes made within the Cookie class --- src/Cookie.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Cookie.php b/src/Cookie.php index 3a3b7d7..a489711 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -75,6 +75,8 @@ class Cookie { */ public function get(string $key, $default = null, $skipHash = false) { + $key = $this->normalizeKey($key); + if ( ! $this->has($key) ) { return $default; } @@ -98,6 +100,8 @@ class Cookie { public function has($key) : bool { + $key = $this->normalizeKey($key); + return array_key_exists($key, $_COOKIE ?? []); } @@ -108,14 +112,16 @@ class Cookie { * @param string URL domain * @return boolean */ - public function delete(string $name, ?string $path = null, ?string $domain = null) { - if ( ! $this->has($name) ) { + public function delete(string $key, ?string $path = null, ?string $domain = null) { + $key = $this->normalizeKey($key); + + if ( ! $this->has($key) ) { return false; } - unset( $_COOKIE[$name] ); + unset( $_COOKIE[$key] ); - return $this->set($name, '', -86400, $path ?: ( $this->options['path'] ?? "" ), $domain ?: ( $this->options['domain'] ?? "" ), $this->options['secure'] ?? false, $this->options['httponly'] ?? false, null, false, true); + return $this->set($key, '', -86400, $path ?: ( $this->options['path'] ?? "" ), $domain ?: ( $this->options['domain'] ?? "" ), $this->options['secure'] ?? false, $this->options['httponly'] ?? false, null, false, true); } public function isSecure($hash, $value) : bool @@ -143,11 +149,16 @@ class Cookie { public function __isset($key) { - return array_key_exists($key, $_SESSION); + return array_key_exists($key, $_COOKIE); } public function __unset($key) { $this->delete($key); } + + protected function normalizeKey(string $key) : string + { + return str_replace(['.', ' ',], '_', $key); + } }