getName(); $cookie->setDomainDefault($this->defaultDomain()); $this->cookies[$cookie->getDomain()][$cookie->getPath()][$name] = $cookie; } /** * Clears a cookie in the browser. * @param string $name * @param string|null $path * @param string|null $domain * @return bool */ protected function unsetCookie(string $name, ?string $path = '/', ?string $domain = null) { if (empty($path)) { $path = '/'; } $domain = $this->defaultDomain($domain); if (isset($this->cookies[$domain][$path][$name])) { unset($this->cookies[$domain][$path][$name]); return true; } return false; } /** * With cookie * @param Cookie $cookie * @return static */ public function withCookie(Cookie $cookie) { $new = clone $this; $new->setCookie($cookie); return $new; } /** * With cookies * @param Cookie[] $cookies * @return static */ public function withCookies(array $cookies) { $new = clone $this; foreach ($cookies as $cookie) { if ($cookie instanceof Cookie) { $new->setCookie($cookie); } } return $new; } /** * Without cookie * @param string $name * @param string|null $path * @param string|null $domain * @return static */ public function withoutCookie(string $name, ?string $path = '/', ?string $domain = null) { $new = clone $this; $new->unsetCookie($name, $path, $domain); return $new; } /** * Delete cookie * @param string $name * @param string|null $path * @param string|null $domain * @param bool $secure * @param bool $httpOnly * @param string|null $sameSite * @return static */ public function deleteCookie(string $name, ?string $path = '/', ?string $domain = null, bool $secure = false, bool $httpOnly = true, ?string $sameSite = null) { $new = clone $this; # try to unset first if ($new->unsetCookie($name, $path, $domain)) { return $new; } $new->setCookie( new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly, false, $sameSite) ); return $new; } /** * Returns an array with all cookies. * @param bool $flat * @return Cookie[] */ public function getCookies($flat = true) { if ($flat) { $flattened = []; foreach ($this->cookies as $path) { foreach ($path as $cookies) { foreach ($cookies as $cookie) { $flattened[] = $cookie; } } } return $flattened; } return $this->cookies; } public function defaultDomain(?string $domain = null) { return $domain ?? '.' . bff('host'); } }