You are here

public function Cookie::__construct in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/http-foundation/Cookie.php \Symfony\Component\HttpFoundation\Cookie::__construct()
  2. 8 vendor/symfony/browser-kit/Cookie.php \Symfony\Component\BrowserKit\Cookie::__construct()
  3. 8 vendor/jcalderonzumba/gastonjs/src/Cookie.php \Zumba\GastonJS\Cookie::__construct()
  4. 8 core/modules/user/src/Authentication/Provider/Cookie.php \Drupal\user\Authentication\Provider\Cookie::__construct()
Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/Cookie.php \Symfony\Component\HttpFoundation\Cookie::__construct()

Constructor.

Parameters

string $name The name of the cookie:

string $value The value of the cookie:

int|string|\DateTime $expire The time the cookie expires:

string $path The path on the server in which the cookie will be available on:

string $domain The domain that the cookie is available to:

bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client:

bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol:

Throws

\InvalidArgumentException

File

vendor/symfony/http-foundation/Cookie.php, line 42

Class

Cookie
Represents a cookie.

Namespace

Symfony\Component\HttpFoundation

Code

public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true) {

  // from PHP source code
  if (preg_match("/[=,; \t\r\n\v\f]/", $name)) {
    throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
  }
  if (empty($name)) {
    throw new \InvalidArgumentException('The cookie name cannot be empty.');
  }

  // convert expiration time to a Unix timestamp
  if ($expire instanceof \DateTime) {
    $expire = $expire
      ->format('U');
  }
  elseif (!is_numeric($expire)) {
    $expire = strtotime($expire);
    if (false === $expire || -1 === $expire) {
      throw new \InvalidArgumentException('The cookie expiration time is not valid.');
    }
  }
  $this->name = $name;
  $this->value = $value;
  $this->domain = $domain;
  $this->expire = $expire;
  $this->path = empty($path) ? '/' : $path;
  $this->secure = (bool) $secure;
  $this->httpOnly = (bool) $httpOnly;
}