You are here

public function CookieJar::setCookie in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/guzzle/src/Cookie/CookieJar.php \GuzzleHttp\Cookie\CookieJar::setCookie()

Sets a cookie in the cookie jar.

Parameters

SetCookie $cookie Cookie to set.:

Return value

bool Returns true on success or false on failure

Overrides CookieJarInterface::setCookie

4 calls to CookieJar::setCookie()
CookieJar::extractCookies in vendor/guzzlehttp/guzzle/src/Cookie/CookieJar.php
Extract cookies from an HTTP response and store them in the CookieJar.
CookieJar::__construct in vendor/guzzlehttp/guzzle/src/Cookie/CookieJar.php
FileCookieJar::load in vendor/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php
Load cookies from a JSON formatted file.
SessionCookieJar::load in vendor/guzzlehttp/guzzle/src/Cookie/SessionCookieJar.php
Load the contents of the client session into the data array

File

vendor/guzzlehttp/guzzle/src/Cookie/CookieJar.php, line 150

Class

CookieJar
Cookie jar that stores cookies an an array

Namespace

GuzzleHttp\Cookie

Code

public function setCookie(SetCookie $cookie) {

  // If the name string is empty (but not 0), ignore the set-cookie
  // string entirely.
  $name = $cookie
    ->getName();
  if (!$name && $name !== '0') {
    return false;
  }

  // Only allow cookies with set and valid domain, name, value
  $result = $cookie
    ->validate();
  if ($result !== true) {
    if ($this->strictMode) {
      throw new \RuntimeException('Invalid cookie: ' . $result);
    }
    else {
      $this
        ->removeCookieIfEmpty($cookie);
      return false;
    }
  }

  // Resolve conflicts with previously set cookies
  foreach ($this->cookies as $i => $c) {

    // Two cookies are identical, when their path, and domain are
    // identical.
    if ($c
      ->getPath() != $cookie
      ->getPath() || $c
      ->getDomain() != $cookie
      ->getDomain() || $c
      ->getName() != $cookie
      ->getName()) {
      continue;
    }

    // The previously set cookie is a discard cookie and this one is
    // not so allow the new cookie to be set
    if (!$cookie
      ->getDiscard() && $c
      ->getDiscard()) {
      unset($this->cookies[$i]);
      continue;
    }

    // If the new cookie's expiration is further into the future, then
    // replace the old cookie
    if ($cookie
      ->getExpires() > $c
      ->getExpires()) {
      unset($this->cookies[$i]);
      continue;
    }

    // If the value has changed, we better change it
    if ($cookie
      ->getValue() !== $c
      ->getValue()) {
      unset($this->cookies[$i]);
      continue;
    }

    // The cookie exists, so no need to continue
    return false;
  }
  $this->cookies[] = $cookie;
  return true;
}