protected static function parseSetCookieHeader($values)
    {
        $cookies = [];
        foreach ($values as $value) {
            $value = rtrim($value, ';');
            $parts = preg_split('/\;[ \t]*/', $value);

            $name = false;
            $cookie = [
                'value' => '',
                'path' => '',
                'domain' => '',
                'secure' => false,
                'httponly' => false,
                'expires' => null,
                'max-age' => null
            ];
            foreach ($parts as $i => $part) {
                if (strpos($part, '=') !== false) {
                    list($key, $value) = explode('=', $part, 2);
                } else {
                    $key = $part;
                    $value = true;
                }
                if ($i === 0) {
                    $name = $key;
                    $cookie['value'] = urldecode($value);
                    continue;
                }
                $key = strtolower($key);
                if (array_key_exists($key, $cookie) && !strlen($cookie[$key])) {
                    $cookie[$key] = $value;
                }
            }
            try {
                $expires = null;
                if ($cookie['max-age'] !== null) {
                    $expires = new DateTimeImmutable('@' . (time() + $cookie['max-age']));
                } elseif ($cookie['expires']) {
                    $expires = new DateTimeImmutable('@' . strtotime($cookie['expires']));
                }
            } catch (Exception $e) {
                $expires = null;
            }

            try {
                $cookies[] = new Cookie(
                    $name,
                    $cookie['value'],
                    $expires,
                    $cookie['path'],
                    $cookie['domain'],
                    $cookie['secure'],
                    $cookie['httponly']
                );
            } catch (Exception $e) {
                // Don't blow up on invalid cookies
            }
        }

        return $cookies;
    }