diff --git a/src/Loader/Http/Cookies/CookieJar.php b/src/Loader/Http/Cookies/CookieJar.php index 3a8d529..f836e36 100644 --- a/src/Loader/Http/Cookies/CookieJar.php +++ b/src/Loader/Http/Cookies/CookieJar.php @@ -123,37 +123,42 @@ protected function getForDomainFromUrl(string|UriInterface|Url $url): ?string protected function buildSetCookieHeaderFromBrowserCookie(BrowserCookie $cookie): string { - $header = $cookie->getName() . '=' . $cookie->getValue(); - - if ($cookie->getDomain() !== null) { - $header .= '; Domain=' . $cookie->getDomain(); - } - - if ($cookie->offsetExists('expires') && $cookie->offsetGet('expires') !== -1) { - $header .= '; Expires=' . $this->formatExpiresValue($cookie->offsetGet('expires')); - } - - if ($cookie->offsetExists('max-age') && !empty($cookie->offsetGet('max-age'))) { - $header .= '; Max-Age=' . $cookie->offsetGet('max-age'); - } - - if ($cookie->offsetExists('path') && !empty($cookie->offsetGet('path'))) { - $header .= '; Path=' . $cookie->offsetGet('path'); - } + $attributes = [ + 'domain' => 'Domain', + 'expires' => 'Expires', + 'max-age' => 'Max-Age', + 'path' => 'Path', + 'secure' => 'Secure', + 'httpOnly' => 'HttpOnly', + 'sameSite' => 'SameSite', + ]; + + $header = [sprintf('%s=%s', $cookie->getName(), $cookie->getValue())]; + + foreach ($attributes as $name => $setCookieName) { + $setCookieValue = $cookie->offsetGet($name); + if ($setCookieValue === null) { + continue; + } - if ($cookie->offsetExists('secure') && $cookie->offsetGet('secure') === true) { - $header .= '; Secure'; - } + // "Expires" attribute + if ($name === 'expires')) { + if ($setCookieValue !== -1) { + $header[] = sprintf('%s=%s', $setCookieName, $this->formatExpiresValue($setCookieValue)); + } + continue; + } - if ($cookie->offsetExists('httpOnly') && $cookie->offsetGet('httpOnly') === true) { - $header .= '; HttpOnly'; - } + // Flag attributes + if ($setCookieValue === true) { + $header[] = $setCookieName; + continue; + } - if ($cookie->offsetExists('sameSite') && !empty($cookie->offsetGet('sameSite'))) { - $header .= '; SameSite=' . $cookie->offsetGet('sameSite'); + $header[] = sprintf('%s=%s', $setCookieName, $setCookieValue); } - return $header; + return implode('; ', $header); } private function formatExpiresValue(mixed $value): string