42 public static function fromString($cookie, $decode =
false)
53 foreach (explode(
';', $cookie) as $part) {
54 if (
false === strpos($part,
'=')) {
58 list($key,
$value) = explode(
'=', trim($part), 2);
62 if (!isset($data[
'name'])) {
63 $data[
'name'] = $decode ? urldecode($key) : $key;
67 switch ($key = strtolower($key)) {
72 $data[
'expires'] = time() + (int)
$value;
80 return new static($data[
'name'], $data[
'value'], $data[
'expires'], $data[
'path'], $data[
'domain'], $data[
'secure'], $data[
'httponly'], $data[
'raw'], $data[
'samesite']);
101 if (preg_match(
"/[=,; \t\r\n\013\014]/",
$name)) {
102 throw new \InvalidArgumentException(sprintf(
'The cookie name "%s" contains invalid characters.',
$name));
106 throw new \InvalidArgumentException(
'The cookie name cannot be empty.');
110 if (
$expire instanceof \DateTimeInterface) {
112 } elseif (!is_numeric(
$expire)) {
116 throw new \InvalidArgumentException(
'The cookie expiration time is not valid.');
125 $this->secure = (bool)
$secure;
127 $this->raw = (bool) $raw;
129 if (
null !== $sameSite) {
130 $sameSite = strtolower($sameSite);
133 if (!in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT,
null),
true)) {
134 throw new \InvalidArgumentException(
'The "sameSite" parameter value is not valid.');
137 $this->sameSite = $sameSite;
149 if (
'' === (
string) $this->
getValue()) {
150 $str .=
'deleted; expires='.gmdate(
'D, d-M-Y H:i:s T', time() - 31536001).
'; max-age=-31536001';
160 $str .=
'; path='.$this->getPath();
164 $str .=
'; domain='.$this->getDomain();
172 $str .=
'; httponly';
176 $str .=
'; samesite='.$this->getSameSite();
229 return 0 !== $this->expire ? $this->expire - time() : 0;
269 return $this->expire < time();
289 return $this->sameSite;