35 private $secureDefault =
false;
37 private static $reservedCharsList =
"=,; \t\r\n\v\f";
38 private static $reservedCharsFrom = [
'=',
',',
';',
' ',
"\t",
"\r",
"\n",
"\v",
"\f"];
39 private static $reservedCharsTo = [
'%3D',
'%2C',
'%3B',
'%20',
'%09',
'%0D',
'%0A',
'%0B',
'%0C'];
49 public static function fromString($cookie, $decode =
false)
62 $part = array_shift($parts);
64 $name = $decode ? urldecode($part[0]) : $part[0];
65 $value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) :
null;
69 if (isset($data[
'max-age'])) {
70 $data[
'expires'] = time() + (int) $data[
'max-age'];
73 return new static(
$name,
$value, $data[
'expires'], $data[
'path'], $data[
'domain'], $data[
'secure'], $data[
'httponly'], $data[
'raw'], $data[
'samesite']);
96 if (9 > \func_num_args()) {
97 @trigger_error(sprintf(
'The default value of the "$secure" and "$samesite" arguments of "%s"\'s constructor will respectively change from "false" to "null" and from "null" to "lax" in Symfony 5.0, you should define their values explicitly or use "Cookie::create()" instead.', __METHOD__), E_USER_DEPRECATED);
101 if ($raw &&
false !== strpbrk(
$name, self::$reservedCharsList)) {
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.');
129 if (
'' === $sameSite) {
131 } elseif (
null !== $sameSite) {
132 $sameSite = strtolower($sameSite);
135 if (!\in_array($sameSite, [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE,
null],
true)) {
136 throw new \InvalidArgumentException(
'The "sameSite" parameter value is not valid.');
139 $this->sameSite = $sameSite;
149 if ($this->
isRaw()) {
152 $str = str_replace(self::$reservedCharsFrom, self::$reservedCharsTo, $this->
getName());
157 if (
'' === (
string) $this->
getValue()) {
158 $str .=
'deleted; expires='.gmdate(
'D, d-M-Y H:i:s T', time() - 31536001).
'; Max-Age=0';
168 $str .=
'; path='.$this->getPath();
172 $str .=
'; domain='.$this->getDomain();
180 $str .=
'; httponly';
184 $str .=
'; samesite='.$this->getSameSite();
237 $maxAge = $this->expire - time();
239 return 0 >= $maxAge ? 0 : $maxAge;
259 return $this->secure ?? $this->secureDefault;
279 return 0 !== $this->expire && $this->expire < time();
299 return $this->sameSite;
307 $this->secureDefault = $default;