@@ -40,6 +40,7 @@ class Cookie
40
40
protected $ secure ;
41
41
protected $ httponly ;
42
42
protected $ rawValue ;
43
+ private $ samesite ;
43
44
44
45
/**
45
46
* Sets a cookie.
@@ -52,8 +53,9 @@ class Cookie
52
53
* @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
53
54
* @param bool $httponly The cookie httponly flag
54
55
* @param bool $encodedValue Whether the value is encoded or not
56
+ * @param string|null $samesite The cookie samesite attribute
55
57
*/
56
- public function __construct (string $ name , ?string $ value , string $ expires = null , string $ path = null , string $ domain = '' , bool $ secure = false , bool $ httponly = true , bool $ encodedValue = false )
58
+ public function __construct (string $ name , ?string $ value , string $ expires = null , string $ path = null , string $ domain = '' , bool $ secure = false , bool $ httponly = true , bool $ encodedValue = false , string $ samesite = null )
57
59
{
58
60
if ($ encodedValue ) {
59
61
$ this ->value = urldecode ($ value );
@@ -67,6 +69,7 @@ public function __construct(string $name, ?string $value, string $expires = null
67
69
$ this ->domain = $ domain ;
68
70
$ this ->secure = $ secure ;
69
71
$ this ->httponly = $ httponly ;
72
+ $ this ->samesite = $ samesite ;
70
73
71
74
if (null !== $ expires ) {
72
75
$ timestampAsDateTime = \DateTime::createFromFormat ('U ' , $ expires );
@@ -106,6 +109,10 @@ public function __toString()
106
109
$ cookie .= '; httponly ' ;
107
110
}
108
111
112
+ if (null !== $ this ->samesite ) {
113
+ $ str .= '; samesite= ' .$ this ->samesite ;
114
+ }
115
+
109
116
return $ cookie ;
110
117
}
111
118
@@ -138,6 +145,7 @@ public static function fromString($cookie, $url = null)
138
145
'secure ' => false ,
139
146
'httponly ' => false ,
140
147
'passedRawValue ' => true ,
148
+ 'samesite ' => null ,
141
149
);
142
150
143
151
if (null !== $ url ) {
@@ -186,7 +194,8 @@ public static function fromString($cookie, $url = null)
186
194
$ values ['domain ' ],
187
195
$ values ['secure ' ],
188
196
$ values ['httponly ' ],
189
- $ values ['passedRawValue ' ]
197
+ $ values ['passedRawValue ' ],
198
+ $ values ['samesite ' ]
190
199
);
191
200
}
192
201
@@ -298,4 +307,14 @@ public function isExpired()
298
307
{
299
308
return null !== $ this ->expires && 0 != $ this ->expires && $ this ->expires < time ();
300
309
}
310
+
311
+ /**
312
+ * Gets the samesite attribute of the cookie.
313
+ *
314
+ * @return string|null The cookie samesite attribute
315
+ */
316
+ public function getSameSite (): ?string
317
+ {
318
+ return $ this ->samesite ;
319
+ }
301
320
}
0 commit comments