@@ -23,14 +23,14 @@ class Cookie
23
23
const SAMESITE_STRICT = 'strict ' ;
24
24
25
25
protected $ name ;
26
- protected $ value ;
27
- protected $ domain ;
28
- protected $ expire ;
29
- protected $ path ;
30
- protected $ secure ;
31
- protected $ httpOnly ;
32
-
33
- private $ raw ;
26
+ protected $ value = null ;
27
+ protected $ domain = null ;
28
+ protected $ expire = 0 ;
29
+ protected $ path = ' / ' ;
30
+ protected $ secure = null ;
31
+ protected $ httpOnly = true ;
32
+
33
+ private $ raw = false ;
34
34
private $ sameSite ;
35
35
private $ secureDefault = false ;
36
36
@@ -41,6 +41,8 @@ class Cookie
41
41
/**
42
42
* Creates cookie from raw header string.
43
43
*
44
+ * @param string $cookie
45
+ * @param bool $decode
44
46
* @return static
45
47
*/
46
48
public static function fromString (string $ cookie , bool $ decode = false )
@@ -90,46 +92,16 @@ public static function create(string $name, string $value = null, $expire = 0, ?
90
92
*/
91
93
public function __construct (string $ name , string $ value = null , $ expire = 0 , ?string $ path = '/ ' , string $ domain = null , bool $ secure = null , bool $ httpOnly = true , bool $ raw = false , ?string $ sameSite = 'lax ' )
92
94
{
93
- // from PHP source code
94
- if ($ raw && false !== strpbrk ($ name , self ::$ reservedCharsList )) {
95
- throw new \InvalidArgumentException (sprintf ('The cookie name "%s" contains invalid characters. ' , $ name ));
96
- }
97
-
98
- if (empty ($ name )) {
99
- throw new \InvalidArgumentException ('The cookie name cannot be empty. ' );
100
- }
101
-
102
- // convert expiration time to a Unix timestamp
103
- if ($ expire instanceof \DateTimeInterface) {
104
- $ expire = $ expire ->format ('U ' );
105
- } elseif (!is_numeric ($ expire )) {
106
- $ expire = strtotime ($ expire );
107
-
108
- if (false === $ expire ) {
109
- throw new \InvalidArgumentException ('The cookie expiration time is not valid. ' );
110
- }
111
- }
112
-
113
- $ this ->name = $ name ;
114
- $ this ->value = $ value ;
115
- $ this ->domain = $ domain ;
116
- $ this ->expire = 0 < $ expire ? (int ) $ expire : 0 ;
117
- $ this ->path = empty ($ path ) ? '/ ' : $ path ;
118
- $ this ->secure = $ secure ;
119
- $ this ->httpOnly = $ httpOnly ;
120
- $ this ->raw = $ raw ;
121
-
122
- if ('' === $ sameSite ) {
123
- $ sameSite = null ;
124
- } elseif (null !== $ sameSite ) {
125
- $ sameSite = strtolower ($ sameSite );
126
- }
127
-
128
- if (!\in_array ($ sameSite , [self ::SAMESITE_LAX , self ::SAMESITE_STRICT , self ::SAMESITE_NONE , null ], true )) {
129
- throw new \InvalidArgumentException ('The "sameSite" parameter value is not valid. ' );
130
- }
131
-
132
- $ this ->sameSite = $ sameSite ;
95
+ $ this
96
+ ->setRaw ($ raw )
97
+ ->setName ($ name )
98
+ ->setValue ($ value )
99
+ ->setExpiresTime ($ expire )
100
+ ->setPath ($ path )
101
+ ->setDomain ($ domain )
102
+ ->setSecure ($ secure )
103
+ ->setHttpOnly ($ httpOnly )
104
+ ->setSameSite ($ sameSite );
133
105
}
134
106
135
107
/**
@@ -190,6 +162,29 @@ public function getName()
190
162
return $ this ->name ;
191
163
}
192
164
165
+ /**
166
+ * Sets the name of the cookie.
167
+ *
168
+ * @param string $name
169
+ * @return $this
170
+ * @throws \InvalidArgumentException
171
+ */
172
+ public function setName (string $ name ): self
173
+ {
174
+ // from PHP source code
175
+ if ($ this ->isRaw () && false !== strpbrk ($ name , self ::$ reservedCharsList )) {
176
+ throw new \InvalidArgumentException (sprintf ('The cookie name "%s" contains invalid characters. ' , $ name ));
177
+ }
178
+
179
+ if (empty ($ name )) {
180
+ throw new \InvalidArgumentException ('The cookie name cannot be empty. ' );
181
+ }
182
+
183
+ $ this ->name = $ name ;
184
+
185
+ return $ this ;
186
+ }
187
+
193
188
/**
194
189
* Gets the value of the cookie.
195
190
*
@@ -200,6 +195,19 @@ public function getValue()
200
195
return $ this ->value ;
201
196
}
202
197
198
+ /**
199
+ * Sets the cookie value.
200
+ *
201
+ * @param string|null $value
202
+ * @return $this
203
+ */
204
+ public function setValue (string $ value = null ): self
205
+ {
206
+ $ this ->value = $ value ;
207
+
208
+ return $ this ;
209
+ }
210
+
203
211
/**
204
212
* Gets the domain that the cookie is available to.
205
213
*
@@ -210,6 +218,19 @@ public function getDomain()
210
218
return $ this ->domain ;
211
219
}
212
220
221
+ /**
222
+ * Sets the domain that the cookie is available to.
223
+ *
224
+ * @param string|null $domain
225
+ * @return $this
226
+ */
227
+ public function setDomain (string $ domain = null ): self
228
+ {
229
+ $ this ->domain = $ domain ;
230
+
231
+ return $ this ;
232
+ }
233
+
213
234
/**
214
235
* Gets the time the cookie expires.
215
236
*
@@ -220,6 +241,31 @@ public function getExpiresTime()
220
241
return $ this ->expire ;
221
242
}
222
243
244
+ /**
245
+ * Sets the time the cookie expires.
246
+ *
247
+ * @param int|string|\DateTimeInterface $expire
248
+ * @return $this
249
+ * @throws \InvalidArgumentException
250
+ */
251
+ public function setExpiresTime ($ expire = 0 ): self
252
+ {
253
+ // convert expiration time to a Unix timestamp
254
+ if ($ expire instanceof \DateTimeInterface) {
255
+ $ expire = $ expire ->format ('U ' );
256
+ } elseif (!is_numeric ($ expire )) {
257
+ $ expire = strtotime ($ expire );
258
+
259
+ if (false === $ expire ) {
260
+ throw new \InvalidArgumentException ('The cookie expiration time is not valid. ' );
261
+ }
262
+ }
263
+
264
+ $ this ->expire = 0 < $ expire ? (int ) $ expire : 0 ;
265
+
266
+ return $ this ;
267
+ }
268
+
223
269
/**
224
270
* Gets the max-age attribute.
225
271
*
@@ -242,6 +288,19 @@ public function getPath()
242
288
return $ this ->path ;
243
289
}
244
290
291
+ /**
292
+ * Sets the path on the server in which the cookie will be available on.
293
+ *
294
+ * @param string|null $path
295
+ * @return $this
296
+ */
297
+ public function setPath (?string $ path ): self
298
+ {
299
+ $ this ->path = empty ($ path ) ? '/ ' : $ path ;
300
+
301
+ return $ this ;
302
+ }
303
+
245
304
/**
246
305
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
247
306
*
@@ -252,6 +311,19 @@ public function isSecure()
252
311
return $ this ->secure ?? $ this ->secureDefault ;
253
312
}
254
313
314
+ /**
315
+ * Makes cookie only be transmitted over a secure HTTPS connection from the client.
316
+ *
317
+ * @param bool|null $secure
318
+ * @return $this
319
+ */
320
+ public function setSecure (bool $ secure = null ): self
321
+ {
322
+ $ this ->secure = $ secure ;
323
+
324
+ return $ this ;
325
+ }
326
+
255
327
/**
256
328
* Checks whether the cookie will be made accessible only through the HTTP protocol.
257
329
*
@@ -262,6 +334,19 @@ public function isHttpOnly()
262
334
return $ this ->httpOnly ;
263
335
}
264
336
337
+ /**
338
+ * Makes cookie accessible only through the HTTP protocol.
339
+ *
340
+ * @param bool $httpOnly
341
+ * @return $this
342
+ */
343
+ public function setHttpOnly (bool $ httpOnly = true ): self
344
+ {
345
+ $ this ->httpOnly = $ httpOnly ;
346
+
347
+ return $ this ;
348
+ }
349
+
265
350
/**
266
351
* Whether this cookie is about to be cleared.
267
352
*
@@ -282,6 +367,19 @@ public function isRaw()
282
367
return $ this ->raw ;
283
368
}
284
369
370
+ /**
371
+ * Toggles cookie value url encoding.
372
+ *
373
+ * @param bool $raw
374
+ * @return $this
375
+ */
376
+ public function setRaw (bool $ raw = false ): self
377
+ {
378
+ $ this ->raw = $ raw ;
379
+
380
+ return $ this ;
381
+ }
382
+
285
383
/**
286
384
* Gets the SameSite attribute.
287
385
*
@@ -292,6 +390,30 @@ public function getSameSite()
292
390
return $ this ->sameSite ;
293
391
}
294
392
393
+ /**
394
+ * Sets the SameSite attribute.
395
+ *
396
+ * @param string|null $sameSite
397
+ * @return $this
398
+ * @throws \InvalidArgumentException
399
+ */
400
+ public function setSameSite (?string $ sameSite = 'lax ' ): self
401
+ {
402
+ if ('' === $ sameSite ) {
403
+ $ sameSite = null ;
404
+ } elseif (null !== $ sameSite ) {
405
+ $ sameSite = strtolower ($ sameSite );
406
+ }
407
+
408
+ if (!\in_array ($ sameSite , [self ::SAMESITE_LAX , self ::SAMESITE_STRICT , self ::SAMESITE_NONE , null ], true )) {
409
+ throw new \InvalidArgumentException ('The "sameSite" parameter value is not valid. ' );
410
+ }
411
+
412
+ $ this ->sameSite = $ sameSite ;
413
+
414
+ return $ this ;
415
+ }
416
+
295
417
/**
296
418
* @param bool $default The default value of the "secure" flag when it is set to null
297
419
*/
0 commit comments