@@ -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
@@ -90,46 +90,16 @@ public static function create(string $name, string $value = null, $expire = 0, ?
90
90
*/
91
91
public function __construct (string $ name , string
8000
$ value = null , $ expire = 0 , ?string $ path = '/ ' , string $ domain = null , bool $ secure = null , bool $ httpOnly = true , bool $ raw = false , ?string $ sameSite = 'lax ' )
92
92
{
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 ;
93
+ $ this
94
+ ->setRaw ($ raw )
95
+ ->setName ($ name )
96
+ ->setValue ($ value )
97
+ ->setExpiresTime ($ expire )
98
+ ->setPath ($ path )
99
+ ->setDomain ($ domain )
100
+ ->setSecure ($ secure )
101
+ ->setHttpOnly ($ httpOnly )
102
+ ->setSameSite ($ sameSite );
133
103
}
134
104
135
105
/**
@@ -190,6 +160,29 @@ public function getName()
190
160
return $ this ->name ;
191
161
}
192
162
163
+ /**
164
+ * Sets the name of the cookie.
165
+ *
166
+ * @return $this
167
+ *
168
+ * @throws \InvalidArgumentException
169
+ */
170
+ public function setName (string $ name ): self
171
+ {
172
+ // from PHP source code
173
+ if ($ this ->isRaw () && false !== strpbrk ($ name , self ::$ reservedCharsList )) {
174
+ throw new \InvalidArgumentException (sprintf ('The cookie name "%s" contains invalid characters. ' , $ name ));
175
+ }
176
+
177
+ if (empty ($ name )) {
178
+ throw new \InvalidArgumentException ('The cookie name cannot be empty. ' );
179
+ }
180
+
181
+ $ this ->name = $ name ;
182
+
183
+ return $ this ;
184
+ }
185
+
193
186
/**
194
187
* Gets the value of the cookie.
195
188
*
@@ -200,6 +193,18 @@ public function getValue()
200
193
return $ this ->value ;
201
194
}
202
195
196
+ /**
197
+ * Sets the cookie value.
198
+ *
199
+ * @return $this
200
+ */
201
+ public function setValue (string $ value = null ): self
202
+ {
203
+ $ this ->value = $ value ;
204
+
205
+ return $ this ;
206
+ }
207
+
203
208
/**
204
209
* Gets the domain that the cookie is available to.
205
210
*
@@ -210,6 +215,18 @@ public function getDomain()
210
215
return $ this ->domain ;
211
216
}
212
217
218
+ /**
219
+ * Sets the domain that the cookie is available to.
220
+ *
221
+ * @return $this
222
+ */
223
+ public function setDomain (string $ domain = null ): self
224
+ {
225
+ $ this ->domain = $ domain ;
226
+
227
+ return $ this ;
228
+ }
229
+
213
230
/**
214
231
* Gets the time the cookie expires.
215
232
*
@@ -220,6 +237,33 @@ public function getExpiresTime()
220
237
return $ this ->expire ;
221
238
}
222
239
240
+ /**
241
+ * Sets the time the cookie expires.
242
+ *
243
+ * @param int|string|\DateTimeInterface $expire
244
+ *
245
+ * @return $this
246
+ *
247
+ * @throws \InvalidArgumentException
248
+ */
249
+ public function setExpiresTime ($ expire = 0 ): self
250
+ {
251
+ // convert expiration time to a Unix timestamp
252
+ if ($ expire instanceof \DateTimeInterface) {
253
+ $ expire = $ expire ->format ('U ' );
254
+ } elseif (!is_numeric ($ expire )) {
255
+ $ expire = strtotime ($ expire );
256
+
257
+ if (false === $ expire ) {
258
+ throw new \InvalidArgumentException ('The cookie expiration time is not valid. ' );
259
+ }
260
+ }
261
+
262
+ $ this ->expire = 0 < $ expire ? (int ) $ expire : 0 ;
263
+
264
+ return $ this ;
265
+ }
266
+
223
267
/**
224
268
* Gets the max-age attribute.
225
269
*
@@ -242,6 +286,18 @@ public function getPath()
242
286
return $ this ->path ;
243
287
}
244
288
289
+ /**
290
+ * Sets the path on the server in which the cookie will be available on.
291
+ *
292
+ * @return $this
293
+ */
294
+ public function setPath (?string $ path ): self
295
+ {
296
+ $ this ->path = empty ($ path ) ? '/ ' : $ path ;
297
+
298
+ return $ this ;
299
+ }
300
+
245
301
/**
246
302
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
247
303
*
@@ -252,6 +308,18 @@ public function isSecure()
252
308
return $ this ->secure ?? $ this ->secureDefault ;
253
309
}
254
310
311
+ /**
312
+ * Makes cookie only be transmitted over a secure HTTPS connection from the client.
313
+ *
314
+ * @return $this
315
+ */
316
+ public function setSecure (bool $ secure = null ): self
317
+ {
318
+ $ this ->secure = $ secure ;
319
+
320
+ return $ this ;
321
+ }
322
+
255
323
/**
256
324
* Checks whether the cookie will be made accessible only through the HTTP protocol.
257
325
*
@@ -262,6 +330,18 @@ public function isHttpOnly()
262
330
return $ this ->httpOnly ;
263
331
}
264
332
333
+ /**
334
+ * Makes cookie accessible only through the HTTP protocol.
335
+ *
336
+ * @return $this
337
+ */
338
+ public function setHttpOnly (bool $ httpOnly = true ): self
339
+ {
340
+ $ this ->httpOnly = $ httpOnly ;
341
+
342
+ return $ this ;
343
+ }
344
+
265
345
/**
266
346
* Whether this cookie is about to be cleared.
267
347
*
@@ -282,6 +362,18 @@ public function isRaw()
282
362
return $ this ->raw ;
283
363
}
284
364
365
+ /**
366
+ * Toggles cookie value url encoding.
367
+ *
368
+ * @return $this
369
+ */
370
+ public function setRaw (bool $ raw = false ): self
371
+ {
372
+ $ this ->raw = $ raw ;
373
+
374
+ return $ this ;
375
+ }
376
+
285
377
/**
286
378
* Gets the SameSite attribute.
287
379
*
@@ -292,6 +384,30 @@ public function getSameSite()
292
384
return $ this ->sameSite ;
293
385
}
294
386
387
+ /**
388
+ * Sets the SameSite attribute.
389
+ *
390
+ * @return $this
391
+ *
392
+ * @throws \InvalidArgumentException
393
+ */
394
+ public function setSameSite (?string $ sameSite = 'lax ' ): self
395
+ {
396
+ if ('' === $ sameSite ) {
397
+ $ sameSite = null ;
398
+ } elseif (null !== $ sameSite ) {
399
+ $ sameSite = strtolower ($ sameSite );
400
+ }
401
+
402
+ if (!\in_array ($ sameSite , [self ::SAMESITE_LAX , self ::SAMESITE_STRICT , self ::SAMESITE_NONE , null ], true )) {
403
+ throw new \InvalidArgumentException ('The "sameSite" parameter value is not valid. ' );
404
+ }
405
+
406
+ $ this ->sameSite = $ sameSite ;
407
+
408
+ return $ this ;
409
+ }
410
+
295
411
/**
296
412
* @param bool $default The default value of the "secure" flag when it is set to null
297
413
*/
0 commit comments