You are here

public static function Cookie::fromString in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/browser-kit/Cookie.php \Symfony\Component\BrowserKit\Cookie::fromString()

Creates a Cookie instance from a Set-Cookie header value.

Parameters

string $cookie A Set-Cookie header value:

string $url The base URL:

Return value

Cookie A Cookie instance

Throws

\InvalidArgumentException

7 calls to Cookie::fromString()
CookieJar::updateFromSetCookie in vendor/symfony/browser-kit/CookieJar.php
Updates the cookie jar from a response Set-Cookie headers.
CookieTest::testFromStringThrowsAnExceptionIfCookieDateIsNotValid in vendor/symfony/browser-kit/Tests/CookieTest.php
CookieTest::testFromStringThrowsAnExceptionIfCookieIsNotValid in vendor/symfony/browser-kit/Tests/CookieTest.php
CookieTest::testFromStringThrowsAnExceptionIfUrlIsNotValid in vendor/symfony/browser-kit/Tests/CookieTest.php
CookieTest::testFromStringWithCapitalization in vendor/symfony/browser-kit/Tests/CookieTest.php

... See full list

File

vendor/symfony/browser-kit/Cookie.php, line 128

Class

Cookie
Cookie represents an HTTP cookie.

Namespace

Symfony\Component\BrowserKit

Code

public static function fromString($cookie, $url = null) {
  $parts = explode(';', $cookie);
  if (false === strpos($parts[0], '=')) {
    throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0]));
  }
  list($name, $value) = explode('=', array_shift($parts), 2);
  $values = array(
    'name' => trim($name),
    'value' => trim($value),
    'expires' => null,
    'path' => '/',
    'domain' => '',
    'secure' => false,
    'httponly' => false,
    'passedRawValue' => true,
  );
  if (null !== $url) {
    if (false === ($urlParts = parse_url($url)) || !isset($urlParts['host'])) {
      throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
    }
    $values['domain'] = $urlParts['host'];
    $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
  }
  foreach ($parts as $part) {
    $part = trim($part);
    if ('secure' === strtolower($part)) {

      // Ignore the secure flag if the original URI is not given or is not HTTPS
      if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) {
        continue;
      }
      $values['secure'] = true;
      continue;
    }
    if ('httponly' === strtolower($part)) {
      $values['httponly'] = true;
      continue;
    }
    if (2 === count($elements = explode('=', $part, 2))) {
      if ('expires' === strtolower($elements[0])) {
        $elements[1] = self::parseDate($elements[1]);
      }
      $values[strtolower($elements[0])] = $elements[1];
    }
  }
  return new static($values['name'], $values['value'], $values['expires'], $values['path'], $values['domain'], $values['secure'], $values['httponly'], $values['passedRawValue']);
}