Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. SetCookie.php

function SetCookie::fromString

Create a new SetCookie object from a string.

Parameters

string $cookie Set-Cookie header string:

1 call to SetCookie::fromString()
CookieJar::extractCookies in vendor/guzzlehttp/guzzle/src/Cookie/CookieJar.php
Extract cookies from an HTTP response and store them in the CookieJar.

File

vendor/guzzlehttp/guzzle/src/Cookie/SetCookie.php, line 35

Class

SetCookie
Set-Cookie object

Namespace

GuzzleHttp\Cookie

Code

public static function fromString(string $cookie) : self {
    // Create the default return array
    $data = self::$defaults;
    // Explode the cookie string using a series of semicolons
    $pieces = \array_filter(\array_map('trim', \explode(';', $cookie)));
    // The name of the cookie (first kvp) must exist and include an equal sign.
    if (!isset($pieces[0]) || \strpos($pieces[0], '=') === false) {
        return new self($data);
    }
    // Add the cookie pieces into the parsed data array
    foreach ($pieces as $part) {
        $cookieParts = \explode('=', $part, 2);
        $key = \trim($cookieParts[0]);
        $value = isset($cookieParts[1]) ? \trim($cookieParts[1], " \n\r\t\x00\v") : true;
        // Only check for non-cookies when cookies have been found
        if (!isset($data['Name'])) {
            $data['Name'] = $key;
            $data['Value'] = $value;
        }
        else {
            foreach (\array_keys(self::$defaults) as $search) {
                if (!\strcasecmp($search, $key)) {
                    if ($search === 'Max-Age') {
                        if (is_numeric($value)) {
                            $data[$search] = (int) $value;
                        }
                    }
                    else {
                        $data[$search] = $value;
                    }
                    continue 2;
                }
            }
            $data[$key] = $value;
        }
    }
    return new self($data);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal