Constructor.
@api
string $name The name of the cookie:
string $value The value of the cookie:
integer|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:
Boolean $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client:
Boolean $httpOnly Whether the cookie will be made accessible only through the HTTP protocol:
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;
}