- Two bugfixes made within the Cookie class

This commit is contained in:
Dave M. 2023-09-14 15:25:50 +00:00
parent fd547dc346
commit 9bf167f782
1 changed files with 16 additions and 5 deletions

View File

@ -75,6 +75,8 @@ class Cookie {
*/ */
public function get(string $key, $default = null, $skipHash = false) public function get(string $key, $default = null, $skipHash = false)
{ {
$key = $this->normalizeKey($key);
if ( ! $this->has($key) ) { if ( ! $this->has($key) ) {
return $default; return $default;
} }
@ -98,6 +100,8 @@ class Cookie {
public function has($key) : bool public function has($key) : bool
{ {
$key = $this->normalizeKey($key);
return array_key_exists($key, $_COOKIE ?? []); return array_key_exists($key, $_COOKIE ?? []);
} }
@ -108,14 +112,16 @@ class Cookie {
* @param string URL domain * @param string URL domain
* @return boolean * @return boolean
*/ */
public function delete(string $name, ?string $path = null, ?string $domain = null) { public function delete(string $key, ?string $path = null, ?string $domain = null) {
if ( ! $this->has($name) ) { $key = $this->normalizeKey($key);
if ( ! $this->has($key) ) {
return false; 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 public function isSecure($hash, $value) : bool
@ -143,11 +149,16 @@ class Cookie {
public function __isset($key) public function __isset($key)
{ {
return array_key_exists($key, $_SESSION); return array_key_exists($key, $_COOKIE);
} }
public function __unset($key) public function __unset($key)
{ {
$this->delete($key); $this->delete($key);
} }
protected function normalizeKey(string $key) : string
{
return str_replace(['.', ' ',], '_', $key);
}
} }